How to Use Structured Exception Handling in .NETHow do you handle errors in your code? Are you using Exception Handling or allowing abrupt error information to pop up to the User? This article explains how to use Structured Exception Handling to handle errors more efficiently. As per .NET terminology, when the normal flow of execution gets affected then an exception has occurred. You can understand this concept with the following examples from C#.
public static
void Main( ){ When you are trying to divide any number by 0, it is obvious thatit is an error. Since you have not handled this situation above, abrupt error message is thrown to User and application is stopped. Instead use try..catch block as shown below. Define your code inside try block and catch exceptions in catch block. public static
void Main( ){ When the exception occurs in try block, catch block defining the most specific exception will be executed. If there is no specific match then catch block handling Exception, the super class of all exceptions will be executed. In this example, which catch block will be executed? Catch block handling DivideByZeroException will be triggered. If the exception occurs inside a function which doesnt handle that exception, then the exception is passed to the Caller, who can handle it in three different ways: Caller Beware: Caller doesnt handle the exception at all. This might lead to problems and User will not be provided sufficient information about the exception. Caller Confuse: Caller catches the exception, does required cleanup and throws the exception. But this does not provide additional information about the exception. Caller Inform: Caller catches the exception, wraps into another exception with additional information recorded and throws it. Handling exceptions alone will not solve your purpose in certain cases. For instance, if you have a file (unmanaged resource) open in your program and an exception occurs even before you have closed the file. Then what happens? Information is provided in the console and the application stops. The file is no longer in use. But the file is still not closed. Hence memory will not be freed. How will you solve this problem? There will be certain block of code that you want to execute irrespective of Exception occurrence. File closing is one such piece of code. You can write such code into finally block, as shown below: public static
void Main( ){ Here testFile.Close() inside try block will never be executed. However code in finally block will be executed and therefore the file will be freed. Till now you were focusing on scenarios which has pre-defined exceptions defined. What if your business scenario requires you to raise an exception very specific to your requirement? For example, you develop banking application wherein the minimum amount in your account after withdrawal should be 10000 bugs. If it is less than that, then you have to throw an exception. .NET provides this facility using User-Defined Exception Classes. Here is an example: public class
customException:Exception{ public class
useCustomException{ Always use exceptions to provide additional information with which you can easily trap the error occurrence. But dont use exceptions for expected events. User Defined Exceptions have a considerable overhead. Hence use them only on need.
|