Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Exception Handling for C# Beginners

1. Introduction to Exceptions
Exception is an object delivered by the Exception class. This Exception class is exposed by
theSystem.Exception namespace. Exceptions are used to avoid system failure in an unexpected
manner.Exception handles the failure situation that may arise. All the exceptions in the .NET
Framework are derived from the System.Exception class.
To understand exception, we need to know two basic things:
An

1.
2.

Somebody sees a failure situation that happened and throws an exception by packing the valid
information.
Somebody who knows that a failure may happen catches the exception thrown. We call
it ExceptionHandler.
Below is a simple example, which shows what happens when an Exception is not handled:
Hide Shrink

Copy Code

class ExceptionsEx
{
//001: Start the Program Execution.
public void StartProgram()
{
Console.WriteLine("Making Call to F1() " );
F1();
Console.WriteLine("Successfully returned from F1() " );
}
//002: Set of Function that calls each other
public void F1()
{
Console.WriteLine("Making Call to F2() " );
throw new System.Exception();
F2();
Console.WriteLine("Successfully returned from F2() " );
}
public void F2()
{
Console.WriteLine("Inside F2 " );
}
//Client 001: Program Entry
[STAThread]
static void Main(string[] args)
{
//Create the Object and start the Execution
ExceptionsEx app = new ExceptionsEx();
app.StartProgram();
}
}

Exception. But, there is no handler to deal with


the thrown exception. This situation is called an Un-Handled exception situation. When you execute the
code, you get an unhandled exception dialog.
In the above code, look at the function F1 that throws an

The above dialog is shown in debug mode, so you may get a chance to break the execution to see where the

exception (not advisable).


In release mode, you will get un-handled Exception in the form of Application crash.
exception is thrown (or) continue ignoring the

So, how do we avoid Un-Handled

Exception? Simple, handle it.

2. Handling Exception
To handle the exception, we need to place the code within the

try block. When an exception occurs inside

thetry block, the control looks for the

catch block and raises an exception that is handled in


the catch block. Below is the simple skeleton for the try and catch block:
Hide Copy Code

try
{
//Some Code that may expected to raise an exception
}
catch
{
//Raised exception Handled here
}

exception. But, the main disadvantage is that we dont know


what exceptionis raised and who raised the exception. Below is the example that handles
the Exception raised by the function F1 and avoids the crash:
The above skeleton handles any

Hide Shrink

class ExceptionsEx
{
//001: Start the Program Execution.

Copy Code

public void StartProgram()


{
Console.WriteLine("Making Call to F1() " );
try
{
F1();
}
catch
{
Console.WriteLine("Some Exception Occurred.
I don't know what Exception it is and where it Occurred. Sorry!");
}
Console.WriteLine("Successfully returned from F1() " );
}
//002: Set of Function that calls each other
public void F1()
{
Console.WriteLine("Making Call to F2() " );
throw new System.Exception();
Console.WriteLine("Successfully returned from F2() " );
}
//Client 001: Program Entry
[STAThread]
static void Main(string[] args)
{
//Create the Object and start the Execution
ExceptionsEx app = new ExceptionsEx();
app.StartProgram();
}
}

You might also like