Syntax
Compared with Java
C# only has try, catch, finally, throw; but java also has throws that mark on method signature.
Example
try{ }catch(Exception ex){ }finally{ // finally is always executed, even if catch statement also throws exception. }
-
Finally throws exception
Exception is not stacked, meaning if the finally also throws exception, then it will override the catch throwed exception.
try{ throw KeyNotFoundException("key not found"); }catch(Exception ex){ throw new AggregateException("", new Exception("Resource Not found.")); }finally{ throw new IndexOutOfRangeException("Out Of Range"); } // the received exception by caller is IndexOutOfRangeException, AggregateException is overriden.
-
Shorthand
try{ }catch{ } // same try{ }catch(Exception e){ } // re-throw the same exception; catch(Exception ex){ throw ex; } // same catch{ throw; }