Try and Catch Blocks in C#

When we develop of an application, there are the following 2 types of errors that can occur in a program:

  1. Compile Time Error
  2. Run Time Error

1. Compile Time Error

The error that is relevant due to a syntactical mistake this error is called a Compile Time error. This type of error is not serious because the error cannot affect your program.

2. Run Time Error

A Runtime Error occurs during program execuation for various reason such as wrong implementation of logic or incorrect input supplied to the program and missing of required resources.

These types of errors can be serious because they can directly affect the program.

Exception Class

An “Exception class” is responsible for abnormal termination of a program whenever the runtime error occurs in the program.

The object of this exception class is responsible for abnormal termination. After abnormal termination they will also display an error message to state the reason for the termination.

Type of Exception

The following are the 2 types of exceptions:

  1. System Exception.
  2. Application Exception.

System Exception

An exception that is raised implicitly of some predefined condition is a “System Exception”, for example DivideByZeroException, FormatException, OverFlowException or NullReference Exception.

Application Exception

An exception that is raised explicitly by the programmer in the application on its own condition is an “Application Exception”.

It can also be referred to as a “User Defind Exception”.

How to Handle Exception

When abnormal termination occurs in the program, the remaining code of the program cannot be executed. If we want to handle these exceptions we need to first stop the abnormal termination and take corrective action to resolve these problems.

Syntax

The following syntax can used for handling an exception.

Try
{
    //statement which causes rruntime error
}
    Catch( <Exception> <var>)
{
    //statement which execute only when runtime error occour
}

Demo

  1. Open Visual Studio 2012.
  2. Select "File" -> "New" -> "Project...".


     
  3. Select the language as “Visual C#” and the template as “Console Application" then provide an appropriate name. I provided the name "ExceptionDemo". Then select the location then click on "OK".


     
  4. By default the name of the class is Program.cs. Rename it to "Demo.cs" by right clicking on "Program.cs" and choose "Rename" then provide the name as "Demo". Then it will ask for conformation; click "OK".


     
  5. Write the following code:


     
  6. Save the program and execute using "F5".

    If we provide a y value as 0 then a DivideByZeroException will occour and the error will be handled and a user-friendly message will be displayed.


In the following output if we provide the non-numeric value then these exceptions will be handled by a FormatException and show the user-friendly error message.



That way we can handle abnormal termination.

Hope you have enjoyed my article.

For more articles visit my blog:

DotNetByAbhiPatil