Constructions
try { ... } catch () { ... } try { ... } catch (Exception e) { ... }
similar in that both will catch every exception that is thrown inside the try block (and if you just don't use this to log exceptions, you should avoid it ). Now let's look at them:
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 just reconstruct the current exception, and this exception will save its "source" and the stack trace.
The third try-catch block is different. When it throws an exception, it will change the trace of the source and stack, so that it appears that the exception was selected from this method from this very line throw e in the method containing this try-catch block.
What benefits should you use? It really depends on each case.
Let's say you have a Person class with a .Save() method that will be stored in the database. Let's say that your application somehow executes the Person.Save() method. If your database refuses to save the Person, then .Save() will throw an exception. Should you use throw or throw e in this case? Well, it depends.
I prefer to do:
try { person.Save(); } catch(DBException e) { throw new InvalidPersonException( "The person has an invalid state and could not be saved!", e); }
This should put a DBException as an "Internal exception" for the new exception. Therefore, when you check for this InvalidPersonException, the stack trace will contain information back to the Save method (this may be enough to solve the problem), but you still have access to the original exception if you need it.
As a final note, when you are expecting an exception, you should really catch that particular exception, not a general Exception , that is, if you expect an InvalidPersonException, which you should prefer:
try { ... } catch (InvalidPersonException e) { ... }
to
try { ... } catch (Exception e) { ... }
Good luck