Exceptions - Exception Handling Date and Issues

I have a couple of questions about exceptions.

1) when you hit the catch block, swallowing means what exactly? I thought it was always retroll or existing exceptions are thrown until the next catch block.

2) If you add Exception.Data values ​​to exccepction, I noticed that I need to do another cast; to subsequently capture that data in another catch block. Why?

+2


source to share


3 answers


  • Swallowing an exception usually means there is a processing block for the exception, but does nothing in the block. For example:

    try {3/0; } catch DivideByZeroException {// ignore} // Note: I know this really doesn't compile because the compiler is smart enough not to let you divide by const.

  • You need to rebuild, because the first handler for the exception will be the only one that gets executed.



If you want the exception to go out of the bubbles, you either can't handle it, or you rebuild it. By the way, it's important to note that in .NET, just saying "throw" will save the stack trace. If you "throw an exception" you will lose the stack trace.

+2


source


Swallowing an exception means catching it and not doing anything useful with it. The general thing you can see is the following:

try
{
     DoSomeOperationThatMightThrow();
}
catch (Exception ex) // don't do this!
{
     // exception swallowed
}

      

Usually you don't want to catch the underlying exception at all, it is better to catch and handle certain types of exceptions and ideally you should only catch the types of exceptions you can do with something useful at the level of the code you are using This can be tricky in complex applications because you can handle different errors at different levels of the code. The highest level of code can simply catch severe / fatal exceptions, while lower levels can catch exceptions which can be dealt with with some error handling logic.



If you catch an exception and need to recover, do the following:

try
{
     DoSomething();
}
catch (SomeException ex)
{
     HandleError(...);

     // rethrow the exception you caught
     throw;

     // Or wrap the exception in another type that can be handled higher up.
     // Set ex as the InnerException on the new one you're throwing, so it
     // can be viewed at a higher level.
     //throw new HigherLevelException(ex);

     // Don't do this, it will reset the StackTrace on ex,
     // which makes it harder to    track down the root issue
     //throw ex;
}

      

+3


source


Ok, you can handle the exception before calling the stack, you can do something like this:


    public class A
    {
        public void methodA()
        {
            try
            {
            }
            catch(Exception e)
            {
                 throw new Exception("Some description", e);
            }
        }
    }

    public class B
    {
        public void methodB()
        {
            try
            {
                 A a = new A();
                 a.methodA();
            }
            catch(Exception e)
            {
                //...here you get exceptions
            }
        }
    }

      

+1


source







All Articles