473,320 Members | 1,977 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,320 software developers and data experts.

Throw an exception

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
6 1994
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
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

"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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kerri | last post by:
Hi, I am new to .NET In my Error Logic on my Aspx pages when an error happens it hits my catch statement where I throw an Exception. My question is : what is the difference between Thwo...
4
by: Eric Lilja | last post by:
Hello, in my program I have a function (pseudo code): void start_mysql_service() { obtain handle start mysql service using handle if start fails close handle and throw an exception...
3
by: Pierre Rouleau | last post by:
The std::exception class defined in the Standard C++ <exception> header specifies that the constructors could throw any exception becuase they do not have a throw() specification. Why is that? ...
0
by: Lasse Vågsæther Karlsen | last post by:
From the book by Jeffrey Richter: throw; will not change the origin of the exception, whereas throw ex; will change the origin of the exception to this statement. When I try the following...
17
by: hplloyd | last post by:
Hi, I have a function that adds data to a database and I want to return true if the database was updated and false if there was a problem, so I am using a try... catch block... My problem is...
6
by: Arjen | last post by:
Hi, I'm reading the enterprise library documentation and there I see the throw statement. try { // run code } catch(Exception ex) {
6
by: Henryk | last post by:
I did a lot of delphi GUI programming recently. In my experience most of the time you just want to throw a standard exception with a descriptive message. Then all calling functions can handle...
28
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't...
6
by: jason.cipriani | last post by:
Consider this program, which defines a template class who's template parameter is the type of an exception that can be thrown by members of the class: === BEGIN EXAMPLE === #include...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.