简体   繁体   中英

C# correct exception handling

I currently have an app that has the following example code path.

    class A
    { 
        private readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(A));
        public void FunctionA()
        {
            try
            {
                B test = new B();
                test.FunctionB();
            }
            catch (Exception ex)
            {
                //handle error ends up here but message is null ref error?
                log.error(ex.Message);
            }
        }
    }
    class B
    {
        private readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(B));
        public void FunctionB()
        {
            try
            {
                //here a custom library function is called that
                //That handles serialized xml data
                //that also has a try catch
            }
            catch (Exception ex)
            {
                //handle error correct error is lost as it falls back to Class A functionA
                log.Error($"Error Encountered During Data operation: {SPID_EX.Message}, \r\n\r\nInner Exception:  {SPID_EX.InnerException.Message}");
            }
        }
    }

The issue is i am trying to handle the error in functionB catch block.

However if an exception is raised in the called library via Function B or during execution in function B, the error ends up in function A catch block; so the handling code i have in function B catch seems to be skipped although when stepping through it enters function B catch reaches log.error... then falls back to function A and no message from B is printed in the logs?

What am i missing here in order to handle the function and continue flow?

Many thanks

EDIT: Added following line to FunctionB based on comments:

log.Info("Testing123");

output:

2018-11-03 01:04:09,361 INFO - Testing123

so seems log is implemented correctly.. :(

Seemingly your log is not initialized, I am not sure what library you are using, however you are not instantiating log correctly and its most likely null .

It is possible this should be DI'ed in, or statically new'ed up for each class depending on the framework

It seems the issue was this line which i failed to add to the example as i didnt suspect it:

log.Error($"Error Encountered During Data operation: {SPID_EX.Message}, \r\n\r\nInner Exception:  {SPID_EX.InnerException.Message}");

It appears that the InnerException.Message was the root cause of this issue so ended up with a null reference error which then passes back to functionA.. I had this in from a previous test due to the root issue being found inside the inner exception so now at least i learned from this and can add a handler for this too.

I appreciate the help from TheGeneral as it caused me to look closer at the implementation.

Appreciate all.

Easy ^^

try
    {
        //...
    }
    catch(Exception ex)
    {
        //Handle a Exception
        throw new Exception("My Exeception Message");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM