Exception Class in C#.Net

This C#.net tutorial, we will discuss exception class in C#.Net and how to write exception to Event Log in Asp.Net C#.Net.

It is always good to do exception and error handling in Asp.Net or C#.Net code. Because it is not a good idea to show the exception message rather we should catch the proper exception and show proper messages to users. .The net language supports structured exception handling. Whenever an error occurred .Net framework creates an exception object which we can use to do exception handling.

Microsoft .Net framework provides The Exception class known as System. Exception. This is the base class for all exceptions. Means every exception is derived from this base class. Some of the classes provided by the DotNet framework are NullReferenceException, IOException, SqlException, DivideByZeroException, ArithmeticException, IOException, SecurityException etc.

The exception has some useful members/properties to know more about the error like InnerException, Message, StackTrace, Source, etc. We can get these like below:

catch (System.Exception ex)
{
string errorMessage = ex.Message;
}
try
{
// Code
}
catch (System.Exception ex)
{
// Error catch block
}
finally
{
// Clean up block.
}

We can write the generic Exception class. But its is better to trap proper exception message. We can trap by using multiple catch block like below.

try
{
// Risky database code goes here.
}
catch (System.Data.SqlClient.SqlException ex)
{
string errorMessage = ex.Message;
// Catch SQL Exception
}
catch (System.NullReferenceException ex)
{
string errorMessage = ex.Message;
// Catches null reference exception.
}
catch (System.Exception ex)
{
string errorMessage = ex.Message;
// Error catch block
}

If the particular exception belongs to SqlException then it will go to the 1st exception block, else if it belongs to nullreference exception then it will go to the 2nd catch block else it will go to the 3rd catch block since it is the generic exception block.

You can also write nested try catch and exception code.

Write exception to Event Log in Asp.Net C#.Net

Now, we will see how to write an exception to Event Log in Asp.Net using C#.Net.

Event log will be accessible from Start -> Run -> eventvwr. To write the exception message in the Event log is now easy in .Net by use of the namespace System.Diagnostics;

Below is the code to write to event log:

using System.Diagnostics;
try
{
// Code
}
catch (System.Exception ex)
{
EventLog log = new EventLog();
log.Source = “Comes from .Net Application”;
log.WriteEntry(ex.Message, EventLogEntryType.Error);
}
finally
{
// Clean up block.
}

Here in the above code the exception message will be written in the event log with source as “Comes from .Net Application”.

Now open the event viewer window ( Start -> Run -> eventvwr ) and Navigate to Windows Logs -> Application, You will able to see the error message.

Exception handling in C#.Net

It is always good to do exception and error handling in Asp.Net or C#.Net code. Because it is not good idea to show exception message rather we should catch the proper exception and show proper messages to users.

.Net language supports structured exception handling. Whenever an error occurred .Net framework creates an exception object which we can use to do exception handling.

Microsoft .Net framework provides The Exception class known as System.Exception. This is the base class for all exceptions. Means every exception is derived from this base class.

Some of the classes provided by DotNet framework are:

  • NullReferenceException
  • IOException
  • SqlException
  • DivideByZeroException
  • ArithmeticException
  • IOException
  • SecurityException etc.

The exception has some useful members/properties to know more about the error like InnerException, Message, StackTrace, Source, etc. We can get these like below:

catch (System.Exception ex)
{
string errorMessage = ex.Message;
}

try
{
// Code
}
catch (System.Exception ex)
{
// Error catch block
}
finally
{
// Clean up block.
}

We can write the generic Exception class. But its is better to trap proper exception message. We can trap by using multiple catch block like below.

try
{
// Risky database code goes here.
}
catch (System.Data.SqlClient.SqlException ex)
{
string errorMessage = ex.Message;
// Catch SQL Exception
}
catch (System.NullReferenceException ex)
{
string errorMessage = ex.Message;
// Catches null reference exception.
}
catch (System.Exception ex)
{
string errorMessage = ex.Message;
// Error catch block
}

If the particular exception belongs to SqlException then it will go to the 1st exception block, else if it belongs to nullreference exception then it will go to the 2nd catch block else it will go to the 3rd catch block since it is the generic exception block.

You can also write nested try catch and exception code.

You may like following C#.Net tutorials:

This C#.Net tutorial explains, exception class in C#.Net and how to write exception to Event Log in Asp.Net C#.Net.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SPGuides.com

>