Exceptions plays very important role in any application. You are expected to throw and catch correct exceptions. In case of custom message you can throw custom Exceptions. I am outlining some important points which might help in Exception handling in your application.
try { } catch (Exception ex) { }
class MyException : Exception { MyException(); MyException(string message) : base(message) { } MyException(string message, Exception innerException) : base(message, innerException) { } }
//Avoid this approach int BadWayOfException() { int i = 0; if (i != 4) throw new InvalidOperationException("wrong value"); else return i + 3; } //right way int RightWayOfManagingException() { int i = 0; if (i != 4) return 0; else return i + 3; }
try { } catch (Exception ex) { throw; }
try { } catch (MyException ex) { } catch (Exception ex) { }
try { } catch (Exception ex) { //passing inner exception throw new InvalidOperationException("my message", ex); }
///<summary> ///<exception cref="InvalidOperationException"></exception> ///</summary> /// <returns></returns>
I hope this document will help you to follow best exception handling practices.
Tags: #.NET, #DOTNET, DotNet, #C#, brh,