Is it the same when I use this:
catch(Exception e)
{
throw new Exception("some Exception", e);
}
and this:
catch(Exception e)
{
throw new Exception("some Exception", e.InnerException);
}
or Di I am missing something?
In your second example you are discarding the caught exception and are using its inner exception instead. You would normally code as you have in the first example.
In the first case, the caught exception (e) becomes the InnerException of the new exception you are throwing:
catch(Exception e)
{
throw new Exception("some Exception", e);
}
In the second case you are using the InnerException of e, and the data from e is lost:
catch(Exception e)
{
throw new Exception("some Exception", e.InnerException);
}
The first example is generally speaking more correct. When you throw a new exception as the result of another exception you should include the original as the inner exception of the new one.
This is not universally true though. It can be useful in some very specific cases where you want to undo another code's wrapping of the real error.
try {
// some reflection call
} catch (TargetInvocationException e) {
// Reduce the layers of data a developer has to dig through by using the real
// exception when I re-wrap
throw new MyException("some message", e.InnerException);
}
There are three ways to re-throw an exception:
catch (Exception ex)
{
throw;
}
Throws the original exception with the original stack trace preserved.
catch (Exception ex)
{
throw ex;
}
Throws the original exception, but the original stack trace is lost. The stack trace now shows this line as the originator of the exception.
catch (Exception ex)
{
throw new Exception("...", ex);
}
Throws a new exception, but preserves the original exception and its stack trace as the InnerException of the new exception.
The first case is useful where you need to clean up on failure but not success. If you need to clean up in both cases you would use a finally clause, not a catch clause. Or you simply want to log that the exception happened here and let the caller deal with it.
The second case is not really useful at all. Why are you hiding the stack trace? There may be some rare circumstance that this is useful, but I know of none.
The third case is most often used when you want to return a single type of exception (or family of types). For example, you are a text parsing method and you want all (non-fatal) exceptions to be turned into ParseException (or whatever) which provides additional detail like the line number in the text where the exception occurred.
In the second example you provide, the exception that was caught is entirely discarded. A new exception is generated and the InnerException of the original exception becomes the InnerException of this new exception. I can imagine no scenario where stripping an intermediate exception would be useful.