Monday, February 17, 2020

Exception handling -> "bubble up

Exception handling -> "bubble up"
Exception handling

(#1)


 try
 {
  ...
  // might fail
  ...
 }
 catch (Exception ex)
 {
  ...
  Log(ex);
  throw ex;
 }
where throw ex bubbles the exception up the chain. Creates a new exception to throw - Stack Trace is truncated below the method that failed, no longer have the full stack trace information.

(#2) Compare to

...
catch (Exception ex)
{
 ...
 throw;
}
...
Here throw "rethrows" exception, and Stack trace information is preserved.

So, if (#1) wants to add user-friendly throw new ApplicationException("failed for certain"), then proper way would be throw new ApplicationException("failed for certain", ex)

more at siccolo blurbs

No comments:

Exception handling -> "bubble up

Exception handling -> "bubble up" Exception handling (#1) try { ... // might fail ... } ca...