473,756 Members | 7,817 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Exception problem!!

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_Names pace;

public MakeConnection
{
SqlConnection Connection;
try
{
Connection = new SqlConnection(" bbababa");
}catch(AppExcep tion e)
{
Response.Write( e.Message.ToStr ing());
}
}

when i execute this, it is not catching the error....it shows the
ArgumentExcepti on 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
Nov 19 '05 #1
6 1763
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********@spa m-i-love-u.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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_Names pace;

public MakeConnection
{
SqlConnection Connection;
try
{
Connection = new SqlConnection(" bbababa");
}catch(AppExcep tion e)
{
Response.Write( e.Message.ToStr ing());
}
}

when i execute this, it is not catching the error....it shows the
ArgumentExcepti on 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

Nov 19 '05 #2
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
ArgumentExcepti on.. 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(Exceptio n e)
{
throw new AppException("C ould 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********@spa m-i-love-u.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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_Names pace;

public MakeConnection
{
SqlConnection Connection;
try
{
Connection = new SqlConnection(" bbababa");
}catch(AppExcep tion e)
{
Response.Write( e.Message.ToStr ing());
}
}

when i execute this, it is not catching the error....it shows the
ArgumentExcepti on 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

Nov 19 '05 #3
Exceptions are caught ONE at a time. If you have a Catch block that is
looking for an AppException, but the ArgumentExcepti on was thrown first, the
AppException will not be caught, as the ArgumentExcepti on 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********@spa m-i-love-u.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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_Names pace;

public MakeConnection
{
SqlConnection Connection;
try
{
Connection = new SqlConnection(" bbababa");
}catch(AppExcep tion e)
{
Response.Write( e.Message.ToStr ing());
}
}

when i execute this, it is not catching the error....it shows the
ArgumentExcepti on 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

Nov 19 '05 #4
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********@spa m-i-love-u.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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_Names pace;

public MakeConnection
{
SqlConnection Connection;
try
{
Connection = new SqlConnection(" bbababa");
}catch(AppExcep tion e)
{
Response.Write( e.Message.ToStr ing());
}
}

when i execute this, it is not catching the error....it shows the
ArgumentExcepti on 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

Nov 19 '05 #5
> 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********@spa m-i-love-u.com> wrote in message
news:Oi******** ******@TK2MSFTN GP14.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********@spa m-i-love-u.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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_Names pace;

public MakeConnection
{
SqlConnection Connection;
try
{
Connection = new SqlConnection(" bbababa");
}catch(AppExcep tion e)
{
Response.Write( e.Message.ToStr ing());
}
}

when i execute this, it is not catching the error....it shows the
ArgumentExcepti on 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


Nov 19 '05 #6
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_Names pace;

public MakeConnection
{
SqlConnection Connection;
try
{
Connection = new SqlConnection(" bbababa");
}catch(AppExcep tion e)
{
Response.Write( e.Message.ToStr ing());
}
}

when i execute this, it is not catching the error....it shows the
ArgumentExcepti on 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
Nov 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1676
by: gh | last post by:
Hi, Is it true that the CLR sometimes can't throw the run time exception? Cause I have just encountered a problem that my program has some unexpected behaviours. After adding try-catch block to many pieces of code. It catches an array index out of bound exception. I am using VS.Net 2003 and is there any way to avoid this? Regards, Aaron
6
7208
by: Adam Hartshorne | last post by:
Hi All, I have the strangest problem, and at an end to try and explain it/know what to do. I have a program written in c++ using visual studio 7.1. It was all working no problems, then after not changing the code or recompiling between two runs using the debugger I all of a sudden got the following error.
11
4224
by: snorkelman | last post by:
I have a case where an exception in the constructor of class with a virtual base leads to termination: struct vbase {}; struct foo : virtual vbase { foo() { throw "exception in foo ctor"; } };
4
41484
by: utab | last post by:
Dear all, I am using boost::lexical_cast to convert string values to real values. (Please read the rest). My question is not related to boost usage or asking a question related to that. When I use that template, an exception is thrown terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct NULL not valid
2
3023
by: Adam Hartshorne | last post by:
Hi All, I have the following problem. I have just installed visual studio 2003 (v7.1) on a new laptop. I also have the same version installed in a desktop pc. As a test, i tried compiling some code I wrote on the desktop pc on the laptop. Now everything compiles and runs ok, expect when I run with the debug on I notice this line in amongst the various Loaded dll lines First-chance exception at 0x7c918fea in age.exe: 0xC0000005: Access
1
1730
by: connor7777 | last post by:
Hi guys: We've been weeding out errors off of a java->c# project and have managed to redeem most of our code with the exception of one bug that we for some reason cannot pin down. The following is an implementation of Linked List class for any item "E" (that also implements IMatchable for itself). It defines "LinkedList" as a recursive datatype with no separate "Cell" or "Node" class, much the same as say for instance ML's Programming...
0
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10040
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9873
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3806
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.