September 27th, 2002, 12:13 AM
|
#1 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 21,018
|
Ok with vb.net they finally have some error handling and one thing I'm unsure of...
I understand try and catch, but what' the deal with finally?
What's the difference between...
Try
DoSomeStuff
Catch
'doh that broke
MsgBox "It broke ya idiot"
Finally
Do some other stuff
End Try
AND....
Try
DoSomeStuff
Catch
'doh that broke
MsgBox "It broke ya idiot"
End Try
Do some other stuff
whats the diff of putting it into the finally, and leaving it out?
According to MSDN Finally runs regardless of the error..
Maybe if there's a fatal error, put code in the finally to close gracefully!?! |
| |
September 27th, 2002, 03:54 PM
|
#2 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 1,542
|
Well, in Java you can have multiple catch blocks for different exceptions; the finally block runs even if you don't provide a catch block for an exception that gets thrown, which means that you can clean up before the method crashes out on you. Maybe this applies to VB.NET? |
| |
September 27th, 2002, 04:04 PM
|
#3 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 21,018
|
Ohhhhh
So its an Else for exception handling
Yup that sounds right.. I was wondering when the java folk were going to swing by and see it, I knew java had true exception handling
Thanks!!
and just so I understand it, if I create a function and I Throw an exception, it should catch it from the calling procedure correct?
I'm assuming yes, but I never did get into the exception thing  |
| |
September 27th, 2002, 04:21 PM
|
#4 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 1,542
|
Yeah...I think so. All I know about exceptions is how to catch them (and I probably wouldn't even know *that* if the Java compiler didn't scream at you whenever you write code that leaves out whatever catch blocks it thinks you need). But yeah, when you throw an exception inside a function, it immediately exits that function and transfers control to the calling function; if the calling statement was in a try block, it looks for a catch statement and if it finds one it runs that block and then the finally block, otherwise it either re-throws the exception or ends the program or thread. Hopefully this makes some sense!  |
| |
September 27th, 2002, 04:26 PM
|
#5 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 21,018
|
piece o' cake 
Although from what I understand (at least with vb.net, but assuming other languages would as well)
The application will still attempt to climb up teh call stack looking for a catch before fully exiting.. so may have to give that a test to see if it actually works
This app I'm writing makes calls out to network shares, so I'm gonna have to make a couple catches looking for network problems.. and for better error handling I may have to throw some exceptions down below in the stack..
Will give it a shot  |
| |
September 30th, 2002, 02:25 AM
|
#6 (permalink)
| | Senior Member
Join Date: Oct 2001
Posts: 881
|
i have found it helpful to use the catch area to print a stack trace of the error if there is an error actually caught. there have been times when data that I used later in the program wasn't what I expected, but I never had a problem with running the code... then I put the error/stacktrace print in the catch area and *poof* I realize that there is an error occuring, but it is not a fatal error. good luck
-Z |
| |
September 30th, 2002, 02:33 AM
|
#7 (permalink)
| | Member
Join Date: Sep 2002
Posts: 364
|
What I understand of it, finally is to do something regardless if an exception occured. I guess it would be useful if your catch block did something closed a form. Say you had a connection open, and maybe you wanted it to close, then I guess you could put it in the finally.
I never use it. |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |