asp net 3.5unleashed

Page 353

§ § §

Many programming languages, including C and early versions of C++, don’t support exceptions. Some developers feel that exceptions are difficult to use and understand, and Microsoft didn’t want to force exceptions down every programmer’s throat. (Personally, I feel the benefit of exceptions far outweighs the “exception handling learning curve.”) Exception handling can hurt the performance of an application. In some situations, exception handling doesn’t perform as well as simply returning an error code. However, if exceptions are rarely thrown, this overhead isn’t noticeable. Again, I feel that the benefit of exceptions outweighs the slight performance hit that sometimes occurs. I also believe that when exception handling is used correctly throughout an application, performance can improve. I’ll address performance later in this chapter. In addition, exceptions in managed code are far less expensive than in some systems, such as C++.

The old ways of reporting violations are too limiting because all the caller gets back is a 32bit number. If the number is the code for an invalid argument, the caller doesn’t know which argument is invalid. And if the number is the code for a division by zero, the caller doesn’t know exactly which line of code caused the division by zero and can’t fix the code easily. Developing software is hard enough without losing important information about problems! If the application code detects a problem, you want all the information possible about the problem so that you can put a proper remedy in place as quickly and easily as possible. The exception handling mechanism offers several advantages over a 32-bit number. An exception includes a string description so that you know exactly which argument is causing the problem. The string might also include additional information giving guidance for improving the code. And an exception includes a stack trace so that you know which path the application took that caused the exception. Another advantage is that exceptions don’t have to be caught or detected at the place they occur. This simplifies coding substantially because you don’t have to associate error detection and correction code with every statement or method call that might fail. Closely related to the previous advantage is perhaps the biggest benefit of all: an exception can’t easily be ignored. If a Win32 function is called and returns a failure code, it’s all too easy for the caller to ignore the return code, assume that the function worked as expected, and allow the application to continue running as if all is fine. However, when a method throws an exception, it’s indicating that the method couldn’t work as expected. If the application doesn’t catch the exception, the CLR terminates the application. To some, this behavior might seem radical and severe. However, I think it’s the right thing to do. If a method is called and an exception is thrown, it’s not OK for the application to continue running. The rest of the application assumes that the previous operations all completed as expected. If this isn’t the case, the application will continue running but with unpredictable results. For example, if the user’s data in the application is corrupt, this data shouldn’t continue to be manipulated. With the Win32 and COM 32-bit numbers, the possibility of the application running with unpredictable results is all too strong. With exceptions, it’s not possible. All the methods defined by types in the Microsoft .NET Framework throw exceptions to indicate that an assumption was violated; no 32-bit status values are returned. So, all programmers must have a full understanding of exceptions and how to handle them in their code. Microsoft made a great decision here! Using exception handling in your code is straightforward and allows you to write code that’s easy to implement, easy to read, and easy to maintain. In addition, exception handling allows you to write robust code that’s capable of recovering from any application situation. Used correctly, exception handling can prevent application crashes and keep your users happy.


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.