Advertisement

Managing Error and Exceptions

By Bintu Chaudhary   Posted at  1:33:00 AM   Error and Exceptions
Home  »  C#  »  Managing Error and Exceptions


     Trapping and handling of runtime errors is one of the most crucial tasks ahead of any programmer. As a developer, you sometimes seem to spend more time checking for errors and handling thme than you do on the core logic of the actual program. You can address this issue by using system exceptions that are designed for the purpose of handling errors.

Types of Errors
An error is a conditon that is not intended in the system. Errors can be of many types and may creep in any phase of system development. Some of the error types are mentioned below:

  • Syntax error
  • Semantic error
  • Logical error
  • Compile-time error
  • Run-time error

Contents

Exceptions

     An Exception is an error condition or unexpected behavior encountered by an executing program during runtime. Errors can happen at almost any time during the compilation or execution of a program. We can detect and deal with these errors using Exception Handling. Exception handling is an in built mechanism in .NET freamework to detect and handle run time errors. At the heart of .NET Framework is the Common Language Runtime (CLR) which is addition to acting as a virtual machine and interpreting and executing IL code on the fly, performs numerous other functions such as type safety checking, memory management, garbage collection and Exception handling.

Syntax of Exception Handling Code

     C# provides three keywords try, catch and finally to do exception handling. The try encloses the statements that might throw an exception whereas catch handles an exception if one exists. The finally can be used for doing any clean up process. The general form try-catch-finally in C# is shown below.

try
{
  // Statement which can cause an exception
}
catch(Type x)
{
  // Statements for handling the exception
}
finally
{
  // Any cleanup code
}

If any exception occurs inside the try block, the control transfers to the appropriate catch block and later to the finally block.

Multiple Catch Statement

     A try block can throw multiple exceptions, which can be handled by using multiple catch block. Remember that more specialized catch block should come before a generalized one. Otherwise the compiler will show a compilation error.




Using Finally Statement

     In C#, both catch and finally blocks are optional. The try block can exist either with one or more catch blocks or a finally block or with both catch and finally blocks.

If there is no exception occurred inside the try block, the control directly transfers to finally block. We can say that the statements inside the finally block is executed always. Note that it is an error to transfer control out of a finally block using break, continue, return or goto.

In C#, it is possible to throw an exception programmatically. The 'throw' keyword is used for this purpose. The general form of throwing an exception is as follows.

throw exception_obj;

For example the following statement throw an ArgumentException explicity.

throw new ArgumentException("Exception")


Output








Throwing Our Own Exceptions

     In C#, it is possible to create our own exception class. But Exception must be the ultimate base class for all exceptions in C#. So the user-defined exception classes must inherit from either Exception class or one of its standard derived classes.






Back to top ↑
Connect with Me