Exceptions


If you are reading this book, you probably have some experience with .NET development or development in general. You are probably also familiar with the concept of exceptions and exception handling. In .NET, exceptions are objects that derive directly or indirectly from System.Exception. Whenever something happens in the code that the developer deems bad, he or she can chose to throw an exception. The following is an example of this in C#:

  // something bad happened, throw a new exception...  ApplicationException myException =    new ApplicationException("Bad stuff happened, game over man!"); throw myException; 

After an exception is thrown, the current method ceases to execute and the exception goes all the way up the call stack until it finds an appropriate exception handler or until the top of the stack is reached. If the latter scenario occurs, the application terminates abruptly with an error message from the .NET runtime.

Good developers not only account for exceptions in situations where something might go wrong, such as talking to a database, but they also are specific in their exception handling. This means you should be looking for very specific exception types when monitoring for exceptions. For example, if your code is performing an elaborate database insert operation, it should be no surprise that if something bad happens, it is probably a database-related error.

In .NET and other modern development platforms, you can specify multiple exceptions handlers for a block of code. Each handler should be looking for a different exception type, and each handler should be more generic than its previous handler. This tells you that the first handlers should be looking for very specific errors that might occur, whereas the latter handlers should be catch-alls. The following is an example of what this looks like in C#:

  try {     // perform some database operation } catch (SqlException ex) {     // this handles an exception specific to MS SQL Server } catch (DataException ex) {     // this handles an exception related to ADO.NET } catch (ApplicationException ex) {     // this handles a custom application exception } catch {     // this is the catch all; most generic } 



Professional Windows Workflow Foundation
Professional Windows Workflow Foundation
ISBN: 0470053860
EAN: 2147483647
Year: 2004
Pages: 118
Authors: Todd Kitta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net