try
{
// Statement which can cause an exception.
}
catch(Type x)
{
// Statements for handling the exception
}
finally
{
//Any cleanup code
}
Exception handling is a mechanisum to identify and deal with the exceptional flows in your code durring the run time. Exception handling in C# uses try, catch, and finally key words.
tryis the block to identify the exceptions.
catchis the block, who is called automatically if any exception occured in the try block.
finally is the block, who is resposible for the the clean-up activities.
throw is the keyword to generate or redirect the exception.
Exceptions are of two types, like exceptions generated by an executing program (Application Exception) or common language runtime (System Exception). System. Exception is the base class for all exceptions.
Exception Class Properties
Exception class properties help to identify the code location, type, exception reason, StackTrace, InnerException, Message, HelpLink, HResult, Source, TargetSite, and Data.
InnerException property maintains the relationship within the two or more exceptions. The OuterException has thrown in response to Inner Exception. The code that handles the outer exception can use the information from the earlier inner exception to handle the error more appropriately.
Supplementary information or description about the current exception has been stored in the Data property.
HelpLink is generally used to provide additional information about the exception. This property can hold URL.
Source maintains the details about the application or object that causes an error or exception.
Best Practices
1. Use throw instead of throw Ex. This will help you to trace the exception to right place.
2. Try to catch as many as Application Exception(s) you can. Never keep your try block unattended.
3. Exceptions should be used to communicate exceptional conditions. Don't use them to communicate events that are expected.
4. Try to use custom Exception only if System. Exception is not enough to support your need.
5. Do not throw an exception as a return code. Throwing an Exception is much more costlier than returing code.
Summary
1. Exceptions are types that all ultimately derive from System.Exception.
2. Use a try block around the statements that might throw exceptions.
3. Once an exception occurs in the try block, the flow of control jumps to the first associated exception handler that is present anywhere in the call stack. In C#, the catch keyword is used to define an exception handler.
4. If no exception handler for a given exception is present, the program stops executing with an error message.
5. Do not catch an exception unless you can handle it and leave the application in a known state. If you catch System.Exception, rethrow it using the throw keyword at the end of the catch block.
6. If a catch block defines an exception variable, you can use it to obtain more information about the type of exception that occurred.
7. Exceptions can be explicitly generated by a program by using the throw keyword.
8. Exception objects contain detailed information about the error, such as the state of the call stack and a text description of the error.
9. Code in a finally block is executed even if an exception is thrown. Use a finally block to release resources, for example to close any streams or files that were opened in the try block.
No comments:
Post a Comment