Let's say we have a class called "WidgetBuilder". We have a method on it to make a Widget: WidgetBuilder.MakeOne(). If something goes wrong inside this method, I throw a custom exception: WidgetException. Now let's say that part of making a Widget is to call the database. If something goes wrong, the methods that I'm calling will throw exceptions specific to their operations (such as a SqlException). Now, part of my code in WidgetBuilder.MakeOne() is a try...catch block. When calls to the database operations fail and throw SqlExceptions, I'll catch them and throw my own WidgetException. Wouldn't it be handy to be able to include the exception that originally happened inside my WidgetException so that I can look thru the exception stack and see where the problem originally occurred. Hence the reason for Exception.InnerException.
try{
SqlClient.SqlConnection objConn = new SqlClient.SqlConnection();
//this will definately break without a connection string
objConn.Open();
}
catch (SqlException e){
throw new WidgetException("Sorry, couldn't build the widget.", e);
}
-
Peter