1

Possible Duplicate:
Why catch and rethrow Exception in C#?

I've been scouring the net trying to find the answer to this question - What's the between the two?

try
{
    //Do Something
}
catch
{
    throw;
}

vs

try
{
    //Do Something
}
catch
{
}

or

try
{
    //Do Something
}
catch (Exception Ex)
{
    //Do something with Exception Ex
}
Community
  • 1
  • 1
user1034912
  • 2,153
  • 7
  • 38
  • 60
  • Did you scour this page? http://msdn.microsoft.com/en-us/library/0yd65esw.aspx – Blorgbeard Jan 30 '12 at 18:22
  • 5
    Eric Lippert gives some guidelines about exception handling in C#: http://blogs.msdn.com/b/ericlippert/archive/2008/09/10/vexing-exceptions.aspx – Elian Ebbing Jan 30 '12 at 18:22
  • +1 on Eric's blog article, it's a good read. – Dynami Le Savard Jan 30 '12 at 18:25
  • but it doesn't explain things in laymen terms... i mean for starters, what does 'rethrow' means? and throw to whom? Anyway thanks for the explanation – user1034912 Jan 30 '12 at 18:33
  • @ElianEbbing: I don't see how Eric Lippert's guidelines could be classified as good. He does not even mention the idea of "throw"ing exceptions, which this SO question is mostly about. – emily_bma Jan 14 '15 at 20:58

6 Answers6

5

The first one rethrows the exception up the stack and preserves the stack trace.

The second one swallows (hides) the exception.

The third one probably also swallows the exception but it depends on what (//Do something) does.

Conrad Frix
  • 51,984
  • 12
  • 96
  • 155
4

catch --> throw will actually just throw your error, so you will have to catch it somewhere else. This can be useful if you want to catch something first and then throw the error to other methods above.

Example:

try
{
    // do something
}
catch
{
    Console.WriteLine("Something went wrong, and you'll know it");
    throw;
}

// won't get here anymore, the exception was thrown.

While try --> catch will just let you ignore the error.

try
{
    // do something
}
catch
{
    Console.WriteLine("Something went wrong, and you won't know it.");
}

// continuing happily
Marnix
  • 6,384
  • 4
  • 43
  • 78
  • It's worth noting that rethrowing without setting the inner exception will effectively swallow the caught expression and hide the full stack trace. – Chris Trombley Jan 30 '12 at 18:30
  • basically... the idea is to make the code fail safe.... (i.e. crash upon error) – user1034912 Jan 30 '12 at 18:31
  • 4
    @ChrisTrombley using the word `throw;` will keep the stack trace as far as I know. using `throw e;` *will* hide the stack trace. – Marnix Jan 30 '12 at 18:34
2

The first one re-throws the same exception that was caught. the second one swallows the exception as if it never happened. So it depends on what you need.

Please note that there IS a difference between these two:

try
{
    //Do Something
}
catch (Exception Ex)
{
    //re-throws the same exception that was caught
    throw;
}

try
{
    //Do Something
}
catch (Exception Ex)
{
    //throws a _new_ exception containing Ex
    throw Ex;
}
jb.
  • 9,921
  • 12
  • 54
  • 90
  • 1
    should note that the first example does include the stack trace from the old exception, while the latter does not. – Tomer W Sep 19 '12 at 15:15
1

In the first, you are just rethrowing the exception. You don't need try/catch in this case, because you aren't doing anything with the caught exception.

In the second, you are swallowing all exceptions. This is incredibly dangerous. Do not ever do this. It is never correct.

In the last, you haven't given us enough detail to know. You are catching the exception, and you might do something with it. You could rethrow, you could swallow. You haven't told us.

jason
  • 236,483
  • 35
  • 423
  • 525
  • What would be the difference between rethrowing the exception (example 1) and not doing any try catch and allowing other exception handlers up the stack deal with the exception? – Greg Osborne Sep 03 '14 at 15:37
1

In the first block, an exception is thrown again inside of the handler for the first block. That means that it can be picked up by an outer-try/catch scope block. The second block "eats" the exception in the catch block, and program execution can continue normally after the code in the catch block is executed.

holtavolt
  • 4,378
  • 1
  • 26
  • 40
1

In the first case the exception is rethrown to the caller. This version is pointless because you do nothing with the exception but in practice you may do something and then throw the exception to the caller. It is also useful when filtering exceptions for example

try
{
   //do somethign
}
catch(Exception1)
{
    throw;
}
catch
{
   //do something
}

The second is something you should never do. You are covering an exception and making it harder to catch eventual bug in your application. You either need to handle the exception in a meaningful way or let it break the program (or let the caller handle it)

The third example is simple exception handing. Make sure you don't use it as a form of if/else (flow control)

Stilgar
  • 22,354
  • 14
  • 64
  • 101