473,386 Members | 1,830 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,386 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 1996
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
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:
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?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.