473,382 Members | 1,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Throwing Exceptions

I have a query about throwing exceptions.

To throw an exception I type something like:

try
{
// do somthing
}
catch (ArgumentOutOfRangeException)
{
throw new Exception("My Exception");
}

And when the try section fales my excepton is thrown and if not court
elswair will display when debuging and hi-light the line:

throw new Exception("My Exception");

but when using other classes like in the microsoft framework library
the line that is hi-lighted is the line that called the class
containing that exception.

What I am trying to say is that when my exception is thrown I no that
it has bean thrown but I don't know what corsed it to throw, so it is
fearly useles being there.

Any sugestions.

Thanks, Jay Dee.

Oct 11 '07 #1
4 1922
couple things i am not sure why you are catching a specific exception and
rethrowing it as a general exception - reason for this? To your question,
what you will need to do is view the exception.Message property and it
should contain the string "my exception" which is normally the reason that
you set when you throw an exception. makes sense?

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless Author Plug
OWC Black Book 2nd Edition
Exclusively on www.lulu.com/owc
$24.99
"Jay Dee" <fi******@gmail.comwrote in message
news:11**********************@g4g2000hsf.googlegro ups.com...
>I have a query about throwing exceptions.

To throw an exception I type something like:

try
{
// do somthing
}
catch (ArgumentOutOfRangeException)
{
throw new Exception("My Exception");
}

And when the try section fales my excepton is thrown and if not court
elswair will display when debuging and hi-light the line:

throw new Exception("My Exception");

but when using other classes like in the microsoft framework library
the line that is hi-lighted is the line that called the class
containing that exception.

What I am trying to say is that when my exception is thrown I no that
it has bean thrown but I don't know what corsed it to throw, so it is
fearly useles being there.

Any sugestions.

Thanks, Jay Dee.

Oct 11 '07 #2
Jay Dee wrote:
[...]
And when the try section fales my excepton is thrown and if not court
elswair will display when debuging and hi-light the line:

throw new Exception("My Exception");

but when using other classes like in the microsoft framework library
the line that is hi-lighted is the line that called the class
containing that exception.
The difference there is basically because you don't have the source code
to the .NET code. So the debugger shows you the source code that is
closest to the thrown exception. If your own code throws an exception,
then that code is the code that threw the exception. But if some other
code throws an exception, then the closest code will still be in your
own code where it called the code for which the source code is absent.
What I am trying to say is that when my exception is thrown I no that
it has bean thrown but I don't know what corsed it to throw, so it is
fearly useles being there.
That's right. As Alvin says, it's not at all clear why you write the
code like that. You had a perfectly good exception. If you're not
going to handle it (that is, recover from it) then why hide it with a
whole new exception? Especially one that is a generic one, rather than
the original one.

At the very least, you could just write "throw;" instead of "throw new
Exception("My Exception");" At least that would cause the exception
thrown to be the same as the one that was caught. If I recall, the
stack trace will be wrong, but if you fix the "catch" statement so that
it declares a local ArgumentOutOfRangeException variable, then at least
you can look at that variable in the debugger and see what the stack
trace is (but only if you put a breakpoint before your own "throw"
statement, of course).

My experience has been that it's almost always a mistake to rethrow an
exception or throw a new exception from a "catch" clause. Sometimes
it's what you want to do, but usually it's better to either just handle
the error locally in the "catch" clause or put whatever cleanup code you
thought should go in the "catch" clause into the "finally" clause
instead, and just not catch any exception.

Pete
Oct 11 '07 #3
What I am trying to say is that when my exception is thrown I no that
it has bean thrown but I don't know what corsed it to throw, so it is
fearly useles being there.
If you want to know what caused the exception, you should check
the call stack. There you can see who called the method which caused
the exception.

piccante
Oct 11 '07 #4
In visual studio, You can break automatically when an exception is thrown,
or is unhandled. The default behavior for the debugger is to break when an
exception is unhandled.

Thus, when you are trying to throw the 'My Exception' below, the debugger
will break the running process. The debugger did not break on the line
inside the try block that threw the 'ArgumentOutOfRangeException' because
the exception was handled.

You can change this by going to Debug | Exceptions. Expand 'Common Language
Runtime Exceptions' and select the exceptions which you want the debugger to
break on as soon as they are thrown.
"Jay Dee" <fi******@gmail.comwrote in message
news:11**********************@g4g2000hsf.googlegro ups.com...
>I have a query about throwing exceptions.

To throw an exception I type something like:

try
{
// do somthing
}
catch (ArgumentOutOfRangeException)
{
throw new Exception("My Exception");
}

And when the try section fales my excepton is thrown and if not court
elswair will display when debuging and hi-light the line:

throw new Exception("My Exception");

but when using other classes like in the microsoft framework library
the line that is hi-lighted is the line that called the class
containing that exception.

What I am trying to say is that when my exception is thrown I no that
it has bean thrown but I don't know what corsed it to throw, so it is
fearly useles being there.

Any sugestions.

Thanks, Jay Dee.
Oct 11 '07 #5

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

Similar topics

5
by: Mark Oueis | last post by:
I've been struggling with this question for a while. What is better design? To design functions to return error codes when an error occures, or to have them throw exceptions. If you chose the...
21
by: mihai | last post by:
People say that is a bad technique to throw exception from constructors; and that the solution would be to create a function _create_ to initialize an object. What about copy constructors? How...
1
by: Farooq Khan | last post by:
i'm writing a class library having following code snippet. i want this class to report an error (by throwing an Exception) to the application using this class library. the problem is that within...
40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
40
by: Sek | last post by:
Is it appropriate to throw exception from a constructor? Thats the only way i could think of to denote the failure of constructor, wherein i am invoking couple of other classes to initialise the...
6
by: Marvin Barley | last post by:
I have a class that throws exceptions in new initializer, and a static array of objects of this type. When something is wrong in initialization, CGI program crashes miserably. Debugging shows...
1
by: usenet | last post by:
I wrote some sample code (see below) for nested exception throwing i.e. my catch blocks are throwing exceptions of their own (for simplicity I used standard exceptions). I am getting some...
3
balabaster
by: balabaster | last post by:
Hey all, this might seem somewhat remedial but I'm unable to find any documentation on it - it pertains to throwing exceptions within a DLL. I've written a DLL that contains a bunch of classes....
21
by: Chris M. Thomasson | last post by:
Is it every appropriate to throw in a dtor? I am thinking about a simple example of a wrapper around a POSIX file... ________________________________________________________________________ class...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.