Tuesday, May 16, 2006

Custom Exceptions

Custom Exceptions

Exceptions are your friends. So is garbage collection :D Well, you shouldn't really ignore Exceptions, in stead, if you just learn yourself a little about them, your code could be easier to understand and easier to debug.

Custom Exceptions allows you to make more narrow, specific Exceptions with customized messages. This often makes it easier to pinpoint where your application fails from a higher persepective. By using InnerExceptions, you can also track back the original error message.

In the following example, I throw my own custom Exception object using the caught DataAdapterException as an InnerException.



try
{
myConnection.Open();
myDataAdapter.Fill(ds_records);
}

catch(Exception err)
{
throw new MyCustomException(
"Database error: Unable to fill dataset", err);
}

finally
{
connection.Close();
}



Now lets make the MyCustomException. First off, lets make a base class _ErrorException which inherits from the Exception class and write the following methods. We'll make the class Serializable, so that .NET is able to serialize the Exception. If the exception is ever used in a remoting context, exceptions on the server are serialized and remoted back to the client proxy. These base constructors are called each time we create a new Exception.



[Serializable]
public class _ErrorException : Exception
{
public string ErrorMessage
{
get
{
return base.Message.ToString();
}
}

public _ErrorException(string errorMessage)
: base(errorMessage) {}

public _ErrorException(string errorMessage, Exception innerEx)
: base(errorMessage, innerEx) {}
}

Now lets create a class MyCustomException which inherits from _ErrorException:

public class MyCustomException : _ErrorException
{
public InternalErrorException(string errorMessage)
: base(errorMessage) {}

public InternalErrorException(string errorMessage, Exception innerEx)
: base(errorMessage, innerEx) {}
}


That's about it. Now, from the business layer of my application, I can track back to the original DataAdapterException and its original error message, but also find out other places in the code affected by the error.

8 comments:

Anonymous said...

Thanks for sharing.
Found exactly what I needed.
*thumbs up*

Anonymous said...

one of the simpler and more elegant solutions, thanks

Anonymous said...

Just a note. As is, this does not compile as the constructors for the exception are named InternalErrorException and not MyCustomException.

Mickaƫl D.S. said...

Nice, thanks!

http://mickaelds.blogspot.com

ARE A EM said...

Thank you for the article.

What is purpose of the _ErrorException class?

Is this example still applicable for C# 2010?

dildar said...

Custom Exception
namespace CustomException{
public class Program{
public static void Main(String[] arg)
{
int a = 10;
try
{
if (a == 10)
{
throw new MyCustomException();
}

}
catch (MyCustomException e)
{

Console.WriteLine(""+e.Message);
}

Console.ReadLine();
}

}
}

class MyCustomException : ApplicationException
{
public MyCustomException()
: base("Divide by Zero Error...")
{

}
public MyCustomException(string message)
: base(message)
{
}
}

http://dildardev.blogspot.com/2011/12/custom-exception.html

aijaz ali said...
This comment has been removed by the author.
ADmin said...

I suppose this is for the reason that it mandates such a variety of new bestessaywriting.org/dissertation-writing-help.php abilities and has additional criticalness because of being key to faultlessness of the last item.