Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between try/catch/throw and try/catch(e)/throw e

What is the difference between

try { } catch { throw; } 

and

try { } catch(Exception e) { throw e;} 

?

And when should I use one or the other?

like image 870
Karim Avatar asked Nov 08 '09 17:11

Karim


People also ask

What is the difference between catch exception e Throw E and catch exception e throw?

Catch and Catch (Exception e) have the same output, and the result is also the same if I write Throw or Throw e. Both are the same because you throw . @MarcGravell - Marc yes if he throws e there is a difference. Got it, I just wanted to re-tag but I see that has already been taken care of!

What is difference between throw and try catch?

Q #1) When to use throws throw VS try-catch in Java? Answer: The “throws” keyword is used to declare the exception with the method signature. The throw keyword is used to explicitly throw the exception. The try-catch block is used to handle the exceptions thrown by others.

What is the difference between throw and throw e?

Unlike throw ex, throw provides all stack information. The main difference between throw and throw ex in C# is that throw provides information about from where the exception was thrown and also about the actual exception while throw ex provides information only about from where the exception was thrown.

What is E in try catch?

'e' is just a variable. ('e' stands for exception, but you can rename it anything you like, however, the data type has to remain 'Exception') The 'e' variable stores an exception-type object in this case.


2 Answers

The constructions

try { ... } catch () { ... } /* You can even omit the () here */  try { ... } catch (Exception e) { ... } 

are similar in that both will catch every exception thrown inside the try block (and, unless you are simply using this to log the exceptions, should be avoided). Now look at these:

try { ... } catch () {     /* ... */     throw; }  try { ... } catch (Exception e) {     /* ... */     throw; }  try { ... } catch (Exception e) {     /* ... */     throw e; } 

The first and second try-catch blocks are EXACTLY the same thing, they simply rethrow the current exception, and that exception will keep its "source" and the stack trace.

The third try-catch block is different. When it throws the exception, it will change the source and the stack trace, so that it will appear that the exception has been thrown from this method, from that very line throw e on the method containing that try-catch block.

Which one should you use? It really depends on each case.

Let's say you have a Person class with a .Save() method that will persist it into a database. Let's say that your application executes the Person.Save() method somewhere. If your DB refuses to save the Person, then .Save() will throw an exception. Should you use throw or throw e in this case? Well, it depends.

What I prefer is doing:

try {     /* ... */     person.Save(); } catch(DBException e) {     throw new InvalidPersonException(        "The person has an invalid state and could not be saved!",        e); } 

This should put the DBException as the "Inner Exception" of the newer exception being throw. So when you inspect this InvalidPersonException, the stack trace will contain info back to the Save method (that might be sufficient for you to solve the problem), but you still have access to the original exception if you need it.

As a final remark, when you are expecting an exception, you should really catch that one specific exception, and not a general Exception, ie, if you are expecting an InvalidPersonException you should prefer:

try { ... } catch (InvalidPersonException e) { ... } 

to

try { ... } catch (Exception e) { ... } 

Good luck!

like image 183
Bruno Reis Avatar answered Sep 28 '22 05:09

Bruno Reis


The first preserves the stack trace while the second resets it. This means that if you use the second approach the stack trace of the exception will always start from this method and you will lose the original exception trace which could be disastrous for someone reading exception logs as he will never find out the original cause of the exception.

The second approach might be useful when you want to add additional information to the stack trace but it is used like this:

try {     // do something } catch (Exception ex) {     throw new Exception("Additional information...", ex); } 

There's a blog post discussing the differences.

like image 44
Darin Dimitrov Avatar answered Sep 28 '22 07:09

Darin Dimitrov