The main advantage of custom exceptions is language support for differentiating between different types of exceptions. for instance
try { SomeFunc() } catch( CustomException EX) { //This is my error that I know how to fix FixThis() DoSomeAwesomeStuff() } catch( Exception exa) { //Somthing else is wrong WeepLikeBaby(); }
If I use the message Property
try { SomeFunc() } catch( Exception exa) { if(exa.Message == "ErrType 1") { DoStuff; } if(exa.Message == "ErrType 2") { Die(); } }
Using the Base enumeration example can save this possibility. However, you give yourself one place to define your messages, but this solution is resolved in various ways. The listing example will make creating localized messages pretty easy, as it will give you a way to define your message strings independently.
Another advantage is that you can add Cusotm data that makes sense in your application. Say, for example, you have an information system for customers, and the customer ID will almost always be important. If you use the message property only, each handler will need to know how to parse this information, if necessary.
public class MyCustomeEx : Exception { int CustID { get; set; } } public void Fail() {
rerun source share