tags:

views:

156

answers:

3

I have a couple questions about exceptions.

1) when you hit a catch block, swallowing means what exactly? I thought it was always rethrow or the existing exceptions is passed up to the next catch block.

2) If you add Exception.Data values to an excepction, I notice I have to do another throw; to grab that data futher up in another catch block later. Why?

+2  A: 
  1. Swallowing an exception normally means having a handling block for the exception, but not doing anything in the block. For example:

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

  2. You have to rethrow because the first handler for an exception is the only one that will execute.

If you want the exception to bubble up you either don't handle it or you rethrow it. By the way, it's important to note that in .NET by just saying "throw" you'll preserve the stack trace. If you "throw Exception" you'll lose your stack trace.

Esteban Araya
right so wtf is this guy talking about?
CoffeeAddict
+1  A: 

Ok, you can handle the exception up to call stack you can do some thing 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
            }
        }
    }
merin
exactly and that's what I did. Thanks.
CoffeeAddict
you don't really need the throw ex. Just throw to preserve.
CoffeeAddict
+2  A: 

Swallowing an exception means catching it and not doing anything useful with it. A common thing you might see is this:

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

You usually don't want to catch a base Exception at all, it's better to catch and handle specific Exception types, and ideally you should only catch exception types that you can do something useful with at the level of code you're in. This can be tricky in complex applications, because you might be handling different errors at different levels in the code. The highest level of code might just catch serious/fatal exceptions, and lower levels might catch exceptions that can be dealt with with some error handling logic.

If you do catch an exception and need to rethrow it, do this:

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;
}
Andy White
I just want someone to verify that the guy who made that statement above in the quotes is an idiot.
CoffeeAddict
I don't like people making statements to me like this when they haven't done their own homework and are wrong. I rethrew the error with throw thus preserving the callstack and data that I had appended to the exception so that I can recatch it later such as in a code-behind class that's calling this method later on and grab that data.
CoffeeAddict
the exception I was able to catch just fine in another catch block futher up was related to an HttpRequest failure, just as I expected when I forced that exception by giving it a bad Uri: System.Net.WebException: The remote name could not be resolved.
CoffeeAddict
so in this case I can't really target specific types with an HttpRequest (You usually don't want to catch a base Exception at all, it's better to catch and handle specific Exception types), who knows what types could be thrown back from an HttpRequest. Many
CoffeeAddict
Maybe he misunderstood what you were doing. It sounds like he thought you were catching an Exception, then throwing a completely new one without passing the caught exception as the Inner... who knows. You're right, sometimes you do need to catch a base Exception, because there are tons of different types of errors that can happen. I try to only catch base Exception at the highest level possible, even if it's just to log and rethrow it. Exception handling is hard...
Andy White
obviously he should have bit his toungue before telling me this in front of others because he doesn't understand throw;
CoffeeAddict