Exceptions and Exception handling in C #

Tram Ho

1. Description

The C # language’s exception handling features help you deal with any unexpected situations or exceptions that occur while a program is running. Exception handling uses the try , catch, and finally keywords to try actions that may fail, to handle failures when you decide it is reasonable to do so, and to clean up resources later. there. Exceptions can be generated by the common language runtime (CLR), .NET, or third-party libraries, or by your application’s code. Exceptions are generated using the throw keyword.

In many cases, an exception can be raised not by a method your code called directly, but by another method deeper in it. When an exception is throw occurs, the CLR will release the memory, searching for a method to sort the catch block specific exception and it will execute the first catch block if found. If it does not find the appropriate catch block anywhere, it will terminate the process and display a message to the user.

In this example, a method checks for division by 0 and catches an error. If there is no exception handler, the program will end up with a SplitByZeroException that has been handled.

2. Exception Overview

The exception has the following properties:

  • Exceptions are all types derived from System.Exception.
  • Using the try block around statements can raise exceptions .
  • When an exception occurs in the try block, the handler goes to the first linked exception handler anywhere in the call stack. In C #, the catch keyword is used to define an exception handler.
  • If there is no exception handler for a certain exception , the program will stop executing with an error message.
  • Don’t catch an exception unless you can handle it and leave the application in a known state. If you catch System.Exception, re-throw it using the throw keyword at the end of the catch block.
  • If a catch block identifies an exception variable, you can use it to get more information about the type of exception that occurred.
  • Exceptions can be explicitly raised by a program using the throw keyword.
  • The exception objects contain details about the error, such as the status of the call stack and description of the error.
  • The code in the finally block is executed even when an exception is thrown. Use a finally block to release a resource, such as to close any stream or file already opened in the try block.

2. Using Exception

In C #, runtime errors are communicated through the program using a mechanism called exception . Exceptions raised by program code crashes and caught by code that can fix the error. Exceptions can be thrown by either the .NET runtime or the program’s code. When an exception is thrown, it is passed to the call stack until a catch statement for the exception is found. Unprocessed exceptions are handled by a generic exception handler provided by the dialog display system.

Exceptions are represented by classes derived from Exceptions. This class specifies the exception type and contains properties that contain exception details. Exporting an exception involves creating an instance of an exception derived class, optionally configuring the exception ‘s properties, and then exporting the object using the throw keyword. For example:

Before the catch block is executed, the run time will check the finally blocks. Finally blocks allow the programmer to clean up any ambiguity states that may be left over from a discarded try block or free any external resources (such as graphics handlers, mechanical connections database or file flow) without waiting for the garbage collector at run time to finalize objects. For example:

If WriteByte () issues an exception, the code in the second try block that tries to reopen the file will fail if the.Close () file is not called and the file will remain locked. Because finally blocks are executed even when an exception is raised, the finally block in the previous example allows the file to be closed correctly and helps avoid errors.

If no compatible catch blocks are found on the call stack after an exception is thrown, one of three things happens:

  • If the exception is in Finalize , the Finalize will be dropped and the base finalizer, if any, is called.
  • If the call stack contains a static constructor or a statuc constructor, then a TypeInitializationException will be thrown, with the original exception assigned to the InnerException property of the new exception .
  • If the thread start point is reached, that thread is terminated.
Share the news now

Source : Viblo