Throw an exception

  • Thread starter Thread starter Fir5tSight
  • Start date Start date
F

Fir5tSight

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
 
Fir5tSight said:
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.
 
"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.

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
 
Curious said:
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.
 
RobinS said:
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.
 
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.
 
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
 
Back
Top