Hi
Can anyone Explaing the Userdefined exceptions with simple understable programming.
what is the use of this UserDefined Exceptions
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Kirtan PatelPosted Dec 28, 2009, 6:25 AM
Here is Detailed Information with Example
if it help you please check "Do you like this answer" check box :)
As you can Also throw Exception Directly By Creating it
Exception ex = new Exception("My Exception");
throw ex;
but if you want more customization solution then this then you can Make your Own Definition that accepts your own error message at runtime then you can code it by inheriting the Exception Class
here in Example we created CountIsZeroExeption that works same but with More Customizability.
using System;
public class UserDefinedExceptionClasses
{
public static void Main()
{
Summer summer = new Summer();
try
{
summer.DoAverage();
}
catch (CountIsZeroException e)
{
Console.WriteLine("CountIsZeroException: {0}", e);
}
}
}
public class CountIsZeroException: ApplicationException
{
public CountIsZeroException()
{
}
public CountIsZeroException(string message)
: base(message)
{
}
public CountIsZeroException(string message, Exception inner)
: base(message, inner)
{
}
}
public class Summer
{
int sum = 0;
int count = 0;
float average;
public void DoAverage()
{
if (count == 0)
throw(new CountIsZeroException("Zero count in DoAverage"));
else
average = sum / count;
}
}
Suresh BabuPosted Mar 22, 2014, 2:11 PM
srinivasPosted Dec 28, 2009, 7:24 AM
When Microsoft created the .NET framework they defined two base classes for system-defined exceptions (SystemException) and for custom exceptions (ApplicationException). However, on reviewing this segregation, Microsoft realised that the benefits that they had expected from the structure were not as expected. For this reason, Microsoft now advises developers to derive custom exceptions directly from the Exception class. This is to keep the hierarchy of exceptions flatter. However, the ApplicationException remains a part of the .NET framework for backwards compatibility at the time of writing.
srinivas reddyPosted Dec 28, 2009, 7:19 AM
here superclass is ApplicactionException na...so y dont we use other superclasses like Exception..etc...is there any rule for that.....and where we have to use different types of superclasses.....
Kirtan PatelPosted Dec 28, 2009, 6:51 AM
Friend., he is not asking for simple constructor but he need information on the Constructor that in in Program i have Posted above :)
Lalit MPosted Dec 28, 2009, 6:48 AM
Use of constructors
More Details
http://www.c-sharpcorner.com/UploadFile/neerajsaluja/ConstructorsInCSharp11152005233222PM/ConstructorsInCSharp.aspx
Kirtan PatelPosted Dec 28, 2009, 6:39 AM
base(message) pass the message to the Constructor of Class that We have inherited ( ApplicationException) it sets the
message to be displayed when exception occur to the Super Class ( ApplicationException) constructor (base
constructor) and Finally Inner Exception is for "what if exception occur in Exception it self " so its set exception there
.. and We have Defined Various Constructors with different parameters because if user want to create its Instance
then he can get Choices According to His Need .
Hope it clears all your doubts !!
and please check "Do you like this answer" check box :)
srinivas reddyPosted Dec 28, 2009, 6:30 AM
public CountIsZeroException(string message, Exception inner)
: base(message, inner)