Custom Exception

R

Raj

To create custom exception whether to inherit ApplicationException or
Exception?

The Message property returns different values if I use ApplicationException
and Exception. What is the difference between using these two??

namespace ConsoleApplication1
{
using System;
class MyException : Exception
{
public MyException(string str)
{
Console.WriteLine("User defined exception");
}

public override string ToString()
{
return Message;
}
}
class MyClient
{
public static void Main()
{
try
{
throw new MyException("Error Encountered");
}
catch (Exception e)
{
Console.WriteLine("Exception caught here" + e.ToString());
}
Console.WriteLine("LAST STATEMENT");
Console.Read();
}
}

}

Thank you

Regards
Raj
 
G

Gregory A. Beamer

To create custom exception whether to inherit ApplicationException or
Exception?

The Message property returns different values if I use
ApplicationException and Exception. What is the difference between
using these two??

namespace ConsoleApplication1
{
using System;
class MyException : Exception
{
public MyException(string str)
{
Console.WriteLine("User defined exception");
}

public override string ToString()
{
return Message;
}
}
class MyClient
{
public static void Main()
{
try
{
throw new MyException("Error Encountered");
}
catch (Exception e)
{
Console.WriteLine("Exception caught here" +
e.ToString());
}
Console.WriteLine("LAST STATEMENT");
Console.Read();
}
}

}

The problem here is not an issue with the type of class you are
inheriting from, as much as a misunderstanding of custom exceptions.

If you are not overriding Message, you need to throw the message down to
the base class. In addition, why override ToString() here so you can get
the Message.

Here is a custom exception coded so the message actually gets placed in
the message:

class MyException : Exception
{
public MyException(string str) : base(str)
{
Console.WriteLine("User defined exception");
}

public override string ToString()
{
return Message;
}
}

here is the same with the Message overridden:

class MyException : Exception
{
private string _message;

public MyException(string str)
{
_message = str;
Console.WriteLine("User defined exception");
}

public override string Message
{
get
{
return _message;
}
}

public override string ToString()
{
return Message;
}
}

Do you see the difference here from what you were trying? In sample 1,
the message is stored in the base object, where it belongs in most
cases. Now the overridden ToString(), which is not wise IMO, pulls the
message.

In sample 2, by overriding the Message property, you accomplish the same
thing, albeit with a bit more code, which is unnecessary here to
accomplish the objective.

The reason you were getting two different results is you were relying on
the underlying default message, which would come out if you simply did
the following:

throw new MyException();

Provided, of course, you had a default constructor. Since you provided a
constructor with message, it is not created and will throw a compiler
error if you change the code to do this.

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
M

Morten Wennevik [C# MVP]

Hi Raj,

ApplicationException is a sub class of Exception, but otherwise identical.
Initially it was meant to serve as a base class for application
specific/custom exceptions. All user thrown exceptions would derive from
ApplicationException and you needed only to catch every ApplicationException
in your outermost layer.

It didn't work out as intended, probably because most people, including me,
tend to use already suitable framework exceptions, which do not derive from
ApplicationException and therefore would not be caught.

Today it is recommended to derive directly from Exception.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top