By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,077 Members | 1,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,077 IT Pros & Developers. It's quick & easy.

Throw an exception

P: n/a
Hi,

What does "throw" do? I understand that it throws an error when
certain exceptional situation happens. My guess is that this *ignores*
the error and *continues* so that it prevents the program from
crashing.

I just don't know where it throws the error to? It makes sense if it
outputs the error into a log file. Please let me know if my thoughts
are correct.

-Emily

Mar 5 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Fir5tSight <fi********@yahoo.comwrote:
What does "throw" do? I understand that it throws an error when
certain exceptional situation happens. My guess is that this *ignores*
the error and *continues* so that it prevents the program from
crashing.

I just don't know where it throws the error to? It makes sense if it
outputs the error into a log file. Please let me know if my thoughts
are correct.
"Throw" throws an exception. The CLR then unwinds the stack, until it
reaches an appropriate "catch" block, where the exception is caught -
so if necessary, it will exit the current method, then exit the calling
method, etc, until it finds a catch block. It will then execute the
catch block.

It's so that you can indicate an error easily without making every
method have a return code to say whether or not it worked.

That's a whirlwind tour - read a book on .NET/C#/VB.NET for much more
information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 5 '07 #2

P: n/a
On Mar 5, 2:10 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
Fir5tSight <fir5tsi...@yahoo.comwrote:
What does "throw" do? I understand that it throws an error when
certain exceptional situation happens. My guess is that this *ignores*
the error and *continues* so that it prevents the program from
crashing.
I just don't know where it throws the error to? It makes sense if it
outputs the error into a log file. Please let me know if my thoughts
are correct.

"Throw" throws an exception. The CLR then unwinds the stack, until it
reaches an appropriate "catch" block, where the exception is caught -
so if necessary, it will exit the current method, then exit the calling
method, etc, until it finds a catch block. It will then execute the
catch block.

It's so that you can indicate an error easily without making every
method have a return code to say whether or not it worked.

That's a whirlwind tour - read a book on .NET/C#/VB.NET for much more
information.

--
Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Hi Jon,

Thanks for the explanation! I just wonder why it's not called "return"
or "report" because that's actually what it does - To return an exact
error to let the programmer know where things go wrong. Why is it
called "throw"? Where does it throw at?

-Emily

Mar 5 '07 #3

P: n/a

"Curious" <fi********@yahoo.comwrote in message
news:11**********************@s48g2000cws.googlegr oups.com...
On Mar 5, 2:10 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
>Fir5tSight <fir5tsi...@yahoo.comwrote:
What does "throw" do? I understand that it throws an error when
certain exceptional situation happens. My guess is that this *ignores*
the error and *continues* so that it prevents the program from
crashing.
I just don't know where it throws the error to? It makes sense if it
outputs the error into a log file. Please let me know if my thoughts
are correct.

"Throw" throws an exception. The CLR then unwinds the stack, until it
reaches an appropriate "catch" block, where the exception is caught -
so if necessary, it will exit the current method, then exit the calling
method, etc, until it finds a catch block. It will then execute the
catch block.

It's so that you can indicate an error easily without making every
method have a return code to say whether or not it worked.

That's a whirlwind tour - read a book on .NET/C#/VB.NET for much more
information.

--
Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeet
Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Hi Jon,

Thanks for the explanation! I just wonder why it's not called "return"
or "report" because that's actually what it does - To return an exact
error to let the programmer know where things go wrong. Why is it
called "throw"? Where does it throw at?

-Emily
As Jon explained in his concise and accurate description, it throws the
exception to the next routine up in the calling stack, and continues to
throw it, until it finds one with a Catch statement. If it doesn't find
any, your application abends. It's just semantics.

Robin S.
Mar 6 '07 #4

P: n/a

"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:4_******************************@comcast.com. ..
>
"Curious" <fi********@yahoo.comwrote in message
news:11**********************@s48g2000cws.googlegr oups.com...
>On Mar 5, 2:10 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
>>Fir5tSight <fir5tsi...@yahoo.comwrote:
What does "throw" do? I understand that it throws an error when
certain exceptional situation happens. My guess is that this
*ignores*
the error and *continues* so that it prevents the program from
crashing.

I just don't know where it throws the error to? It makes sense if it
outputs the error into a log file. Please let me know if my thoughts
are correct.

"Throw" throws an exception. The CLR then unwinds the stack, until it
reaches an appropriate "catch" block, where the exception is caught -
so if necessary, it will exit the current method, then exit the calling
method, etc, until it finds a catch block. It will then execute the
catch block.

It's so that you can indicate an error easily without making every
method have a return code to say whether or not it worked.

That's a whirlwind tour - read a book on .NET/C#/VB.NET for much more
information.

--
Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeet
Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Hi Jon,

Thanks for the explanation! I just wonder why it's not called "return"
or "report" because that's actually what it does - To return an exact
error to let the programmer know where things go wrong. Why is it
called "throw"? Where does it throw at?

-Emily

As Jon explained in his concise and accurate description, it throws the
exception to the next routine up in the calling stack, and continues to
throw it, until it finds one with a Catch statement. If it doesn't find
any, your application abends. It's just semantics.

Robin S.
Here's an example. I'm mocking this up, so if it's not perfect, at least
you'll get the general idea; most of it is in pseudocode.

in UpdateForm / Save Click event
Try
myBusinessLayer.myObject.Save
Catch ex as Exception
MessageBox.Show("That update didn't work. You're *so* S.O.L.")
End Try

in myBusinessLayer.myObject.Save
unload the fields from the form and put them in SQL Parameters
call myDataAccessLayer.SaveMyObject

in myDataAccessLayer.SaveMyObject
set up the SQLDataAdapter, connection, etc.
Try
myAdapter.Update()
Catch ex as Exception
'oh, darn, there was an error, let the user figure it out
'throw it up the stack
Throw ex
End Try

In the last routine, it throws the exception up the stack. So it goes to
myBusinessLayer.myObject.Save and finds no Catch block.

So it goes up to UpdateForm/Save Click event, where it finds a catch, and
performs the code in Catch block.

Hope (a) this helps, and (b) I got it right. I'm sure Jon or someone else
will correct me if I didn't. :-O

Robin S.
Mar 6 '07 #5

P: n/a
Hello,

I would had that generally, you catch an exception in business layer, then
you may do something (retry, or log it, or whatever) and sometimes you want
to throw it to the presentation layer for the user. then you need throw.

hope this helps.

"RobinS" <Ro****@NoSpam.yah.nonea écrit dans le message de
news:ju******************************@comcast.com. ..
>
"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:4_******************************@comcast.com. ..
>>
"Curious" <fi********@yahoo.comwrote in message
news:11**********************@s48g2000cws.googleg roups.com...
>>On Mar 5, 2:10 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
Fir5tSight <fir5tsi...@yahoo.comwrote:
What does "throw" do? I understand that it throws an error when
certain exceptional situation happens. My guess is that this
*ignores*
the error and *continues* so that it prevents the program from
crashing.

I just don't know where it throws the error to? It makes sense if it
outputs the error into a log file. Please let me know if my thoughts
are correct.

"Throw" throws an exception. The CLR then unwinds the stack, until it
reaches an appropriate "catch" block, where the exception is caught -
so if necessary, it will exit the current method, then exit the calling
method, etc, until it finds a catch block. It will then execute the
catch block.

It's so that you can indicate an error easily without making every
method have a return code to say whether or not it worked.

That's a whirlwind tour - read a book on .NET/C#/VB.NET for much more
information.

--
Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeet
Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Hi Jon,

Thanks for the explanation! I just wonder why it's not called "return"
or "report" because that's actually what it does - To return an exact
error to let the programmer know where things go wrong. Why is it
called "throw"? Where does it throw at?

-Emily

As Jon explained in his concise and accurate description, it throws the
exception to the next routine up in the calling stack, and continues to
throw it, until it finds one with a Catch statement. If it doesn't find
any, your application abends. It's just semantics.

Robin S.

Here's an example. I'm mocking this up, so if it's not perfect, at least
you'll get the general idea; most of it is in pseudocode.

in UpdateForm / Save Click event
Try
myBusinessLayer.myObject.Save
Catch ex as Exception
MessageBox.Show("That update didn't work. You're *so* S.O.L.")
End Try

in myBusinessLayer.myObject.Save
unload the fields from the form and put them in SQL Parameters
call myDataAccessLayer.SaveMyObject

in myDataAccessLayer.SaveMyObject
set up the SQLDataAdapter, connection, etc.
Try
myAdapter.Update()
Catch ex as Exception
'oh, darn, there was an error, let the user figure it out
'throw it up the stack
Throw ex
End Try

In the last routine, it throws the exception up the stack. So it goes to
myBusinessLayer.myObject.Save and finds no Catch block.

So it goes up to UpdateForm/Save Click event, where it finds a catch, and
performs the code in Catch block.

Hope (a) this helps, and (b) I got it right. I'm sure Jon or someone else
will correct me if I didn't. :-O

Robin S.
Mar 6 '07 #6

P: n/a
Hi Robin,

Thanks for the example! I now get a better picture about the "throw"
thing. It throws because somewhere else needs to "catch" it.

-Emily

Mar 6 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.