I've a problem in handling a custom exception
The following is my custom exception class:
public class AppException : public Exception
{
public AppException (string message, Exception innerException)
{ }
public override string Message
{
get {
return base.Message + "Blahblah..";
}
}
}
This is my implementation:
Using Exception_Namespace;
public MakeConnection
{
SqlConnection Connection;
try
{
Connection = new SqlConnection("bbababa");
}catch(AppException e)
{
Response.Write(e.Message.ToString());
}
}
when i execute this, it is not catching the error....it shows the
ArgumentException which is because
of the wrong connection string has been passed. I'm expecting the "blah.."
string added to the overriden
message property of AppException class.
Rather, If i use Exception instead of AppException it is working fine.
So, Why it is not working when i'm using AppException? What is wrongness i'm
doing here?
Thanks in Advance
Vadivel Kumar 6 1641
you can only catch an exception of the type that has been thrown (or a
superclass).
SqlException (the exception that is being thrown) is a subclass of
Exception, hence catching Exception will also catch SqlException. However,
SqlException is not a subclass of AppException and there is no way you can
make it so, so this will not work.
I'm not sure what you are trying to achieve by doing this. However, I expect
you should look up exception wrapping. This is a common practice for
creating your own exception that wraps another exception (i.e. holds a
handle to the underlying exception)
Andy
"Vadivel Kumar" <do********@spam-i-love-u.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... I've a problem in handling a custom exception
The following is my custom exception class:
public class AppException : public Exception { public AppException (string message, Exception innerException) { }
public override string Message { get { return base.Message + "Blahblah.."; } } }
This is my implementation:
Using Exception_Namespace;
public MakeConnection { SqlConnection Connection; try { Connection = new SqlConnection("bbababa"); }catch(AppException e) { Response.Write(e.Message.ToString()); } }
when i execute this, it is not catching the error....it shows the ArgumentException which is because of the wrong connection string has been passed. I'm expecting the "blah.." string added to the overriden message property of AppException class.
Rather, If i use Exception instead of AppException it is working fine.
So, Why it is not working when i'm using AppException? What is wrongness i'm doing here?
Thanks in Advance Vadivel Kumar
Vadivel:
The reason it doesn't work when you specify catch (AppException e) is
because that statement is saying "only catch the exeption if it's of type
AppException. As you know, it isn't of type AppException but rather
ArgumentException.. Nothing in the framework will throw an exception of the
type of your custom exception, what you typically do with custom exception
is throw them yourself, this would be a far more logical usage of your
custom exception:
try
{
Connection = new SqlConnection("bbababa");
}catch(Exception e)
{
throw new AppException("Could not create connection", e);
}
notice how I repackage e inside your custom exception. Once this exception
is thrown, you can then catch AppException at a higher level (since this is
now the type of your exception).. Before you start rethrowing exceptions
left and right, make sure to check out: http://dotnetguy.techieswithcats.com...s/004118.shtml
Karl
--
MY ASP.Net tutorials http://www.openmymind.net/
"Vadivel Kumar" <do********@spam-i-love-u.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... I've a problem in handling a custom exception
The following is my custom exception class:
public class AppException : public Exception { public AppException (string message, Exception innerException) { }
public override string Message { get { return base.Message + "Blahblah.."; } } }
This is my implementation:
Using Exception_Namespace;
public MakeConnection { SqlConnection Connection; try { Connection = new SqlConnection("bbababa"); }catch(AppException e) { Response.Write(e.Message.ToString()); } }
when i execute this, it is not catching the error....it shows the ArgumentException which is because of the wrong connection string has been passed. I'm expecting the "blah.." string added to the overriden message property of AppException class.
Rather, If i use Exception instead of AppException it is working fine.
So, Why it is not working when i'm using AppException? What is wrongness
i'm doing here?
Thanks in Advance Vadivel Kumar
Exceptions are caught ONE at a time. If you have a Catch block that is
looking for an AppException, but the ArgumentException was thrown first, the
AppException will not be caught, as the ArgumentException is unhandled.
Exception is the base class for ALL Exceptions, so it will catch whichever
Exception happens first.
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
"Vadivel Kumar" <do********@spam-i-love-u.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... I've a problem in handling a custom exception
The following is my custom exception class:
public class AppException : public Exception { public AppException (string message, Exception innerException) { }
public override string Message { get { return base.Message + "Blahblah.."; } } }
This is my implementation:
Using Exception_Namespace;
public MakeConnection { SqlConnection Connection; try { Connection = new SqlConnection("bbababa"); }catch(AppException e) { Response.Write(e.Message.ToString()); } }
when i execute this, it is not catching the error....it shows the ArgumentException which is because of the wrong connection string has been passed. I'm expecting the "blah.." string added to the overriden message property of AppException class.
Rather, If i use Exception instead of AppException it is working fine.
So, Why it is not working when i'm using AppException? What is wrongness i'm doing here?
Thanks in Advance Vadivel Kumar
I think as Andy Fish told, the SqlException class only inherits the
Exception
not its child class AppException, so it could not catch the error.
I slightly disagree with the Kevin's statement b'coz even if the excepion
catch is occured one
at a time, it should catch the exception as it (AppException) is inheriting
the
Exception class as like SqlConnection.
I know, that i dont have definetive or logic approach to proceed
with this issue. So, better advice me in handling the exceptions
in better way.
Basically, The AppException class has to log the errors which is caught by
the
parent Exception class and reformat it for presenting
with a solution in the presentation layer.
So, Advice me...
Cheers!
Vadivel Kumar
"Vadivel Kumar" <do********@spam-i-love-u.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... I've a problem in handling a custom exception
The following is my custom exception class:
public class AppException : public Exception { public AppException (string message, Exception innerException) { }
public override string Message { get { return base.Message + "Blahblah.."; } } }
This is my implementation:
Using Exception_Namespace;
public MakeConnection { SqlConnection Connection; try { Connection = new SqlConnection("bbababa"); }catch(AppException e) { Response.Write(e.Message.ToString()); } }
when i execute this, it is not catching the error....it shows the ArgumentException which is because of the wrong connection string has been passed. I'm expecting the "blah.." string added to the overriden message property of AppException class.
Rather, If i use Exception instead of AppException it is working fine.
So, Why it is not working when i'm using AppException? What is wrongness i'm doing here?
Thanks in Advance Vadivel Kumar
> I slightly disagree with the Kevin's statement b'coz even if the excepion catch is occured one at a time, it should catch the exception as it (AppException) is inheriting the Exception class as like SqlConnection.
You can disagree with me all you want, but what I told you was true. It
doesn't matter what you agree with; it only matters what is true. If you
disagree with the truth, the truth remains. You, on the other hand, are
working on a false assumption.
Let me explain: When an exception occurs, processing is halted at the point
where the Exception occurs. If a Try/Catch block catches the exception, note
that the rest of the code in the Try block is NOT executed. Instead, control
falls through to the Catch block. If the exception is NOT handled, program
execution halts. Therefore, only ONE exception at a time can occur.
Now, you claim that since your AppExceptionn inherits Exception that it
should be caught. However, that is simply not true. Let's use a real-world
example to make it more clear:
A Ford is a car. A Toyota is a car. both car types "inherit" car. However, a
Ford is not a Toyota, and a Toyota is not a Ford. So, if you have a bunch of
cars, and you want to "catch" some of them, you could give an instruction to
catch "car," and both Fords and Toyotas would be "caught." On the other
hand, if you said "Catch Fords" would Toyotas be caught? No. Why? Because
although a Toyota is a car, it is not a Ford. So, if you said "Catch Fords"
would ANY "car" be caught? No. Why? Because Fords aren't the only type of
car. A Ford is a car, but a car is not necessarily a Ford. An AppException
is an Exception, but an Exception is not necessarily an AppException. So, if
you say "Catch AppException" that is the only kind of Exception that will be
caught (or any type that inherits AppException). It is specific. Exception
is general, just as "Ford" is specific, and "car" is general. The catch
block is like a filter. It specifies exactly what type of Exception should
be caught. Anything that fits that type will be caught. Exception is not
AppException, but AppException is Exception. Inheritance only works one way.
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
"Vadivel Kumar" <do********@spam-i-love-u.com> wrote in message
news:Oi**************@TK2MSFTNGP14.phx.gbl...I think as Andy Fish told, the SqlException class only inherits the Exception not its child class AppException, so it could not catch the error.
I slightly disagree with the Kevin's statement b'coz even if the excepion catch is occured one at a time, it should catch the exception as it (AppException) is inheriting the Exception class as like SqlConnection.
I know, that i dont have definetive or logic approach to proceed with this issue. So, better advice me in handling the exceptions in better way.
Basically, The AppException class has to log the errors which is caught by the parent Exception class and reformat it for presenting with a solution in the presentation layer.
So, Advice me...
Cheers! Vadivel Kumar
"Vadivel Kumar" <do********@spam-i-love-u.com> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl... I've a problem in handling a custom exception
The following is my custom exception class:
public class AppException : public Exception { public AppException (string message, Exception innerException) { }
public override string Message { get { return base.Message + "Blahblah.."; } } }
This is my implementation:
Using Exception_Namespace;
public MakeConnection { SqlConnection Connection; try { Connection = new SqlConnection("bbababa"); }catch(AppException e) { Response.Write(e.Message.ToString()); } }
when i execute this, it is not catching the error....it shows the ArgumentException which is because of the wrong connection string has been passed. I'm expecting the "blah.." string added to the overriden message property of AppException class.
Rather, If i use Exception instead of AppException it is working fine.
So, Why it is not working when i'm using AppException? What is wrongness i'm doing here?
Thanks in Advance Vadivel Kumar
Vadivel Kumar wrote: I've a problem in handling a custom exception
The following is my custom exception class:
public class AppException : public Exception { public AppException (string message, Exception innerException) { }
public override string Message { get { return base.Message + "Blahblah.."; } } }
This is my implementation:
Using Exception_Namespace;
public MakeConnection { SqlConnection Connection; try { Connection = new SqlConnection("bbababa"); }catch(AppException e) { Response.Write(e.Message.ToString()); } }
when i execute this, it is not catching the error....it shows the ArgumentException which is because of the wrong connection string has been passed. I'm expecting the "blah.." string added to the overriden message property of AppException class.
Rather, If i use Exception instead of AppException it is working fine.
So, Why it is not working when i'm using AppException? What is wrongness i'm doing here?
Thanks in Advance Vadivel Kumar
"catch (AppException e)" does not mean "catch any exception and wrap
it in my own AppException", it means "catch only AppExceptions (or
exceptions derived of it) and ignore others".
--
Hans Kesting This discussion thread is closed Replies have been disabled for this discussion. Similar topics
5 posts
views
Thread by gh |
last post: by
|
6 posts
views
Thread by Adam Hartshorne |
last post: by
|
11 posts
views
Thread by snorkelman |
last post: by
|
4 posts
views
Thread by utab |
last post: by
|
2 posts
views
Thread by Adam Hartshorne |
last post: by
| | | | | | | | | | | |