BleepingComputer.com: Visual Basic programming

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Visual Basic programming Try-Catch return codes

#1 User is offline   KermitD 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 81
  • Joined: 28-April 05
  • Location:Stelle, Illinois

Posted 06 January 2011 - 01:02 AM

I have read many places about the Try-Catch function (or method or whatever) and seen a number of examples. It goes:

Try
some code
Catch
some code
Return (a code number)

The return code will be 0, -1, -2, -3, etc.

None of the articles I have read about Try-Catch explain why one code or another is used, even though the accompanying examples use different code numbers. Can anyone tell me when to use which code? Or give me a link?

Thanks,
Kermit
KermitD
Choices have consequences.

#2 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,605
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 06 January 2011 - 08:38 AM

Your nomenclature is a bit off; it should be:
Try
   //do something here
Catch ex As Exception
   //handle the error condition
End Try



When handling the error condition, you can do whatever it is you want to do. A try-catch block is meant to prevent a program from bombing out if an error occurs; the program will just keep going. In your catch block, you may simply want to write an error message to a log file, or you may want to return some value that indicates an error. Or you may want to redirect to another function to fix whatever may have caused the error condition in the first place.

The actual return value is up to you, and it can mean whatever you want it to mean. Typically a return value of zero means that the code succeeded, a one means the code failed. If you wanted, you could return 'foo' to mean success, and 'bar' to indicate an error.

This post has been edited by groovicus: 06 January 2011 - 10:52 AM

"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#3 User is offline   KermitD 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 81
  • Joined: 28-April 05
  • Location:Stelle, Illinois

Posted 06 January 2011 - 10:11 AM

Groovicus, are you saying that the value of the Return code is arbitrary? In the example below could I use 0 for all 3 return codes? If not - why not? How about 0, -2, -4 instead of 0, -1, -2? Doesn't the value of the Return code mean anything? Because if it doesn't why use a Return statement at all?

In the below example the user has already typed a file name(FN)

Try
Dim theFile as FileStream
theFile = File.OpenRead(FN)
Return 0
Catch ex As FileNotFoundException
MsgBox("The system could not find the file" & vbNewline & "Is the name correct?")
Return -1
Catch ex As DirectoryNotFoundException
MsgBox("The directory you entered isn't correct.")
Return -2
End Try

This post has been edited by KermitD: 06 January 2011 - 10:20 AM

KermitD
Choices have consequences.

#4 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,605
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 06 January 2011 - 10:52 AM

You use different return codes to indicate different errors, but the numbers are completely arbitrary. Yes, you could use a zero for all of them if you wanted to, although that is not standard. Zeros and ones are the equivalent of true and false, or false (0) and true (1) to be more precise. Actually, almost anything other than zero will resolve to true, but that is a different topic.

The meaning if the value is determined by the programmer. For example, let's say that your try-catch statement is inside a function called 'doesFileExist'. Somewhere you have a statement like this, and ignoring the fact that I am not using proper vb syntax:

i = doesFileExist();
if( i == 0)
   // everything is cool, go on
if( i == -1)
   // print an error message indicating the problem (file not found)
if( i == -2)
   // print an error message indicating the problem ( directory not found)


The reason you would do something like this is to keep your code short and compartmentalized. Our imaginary function's only responsibility should be to check and see if a file exists. It should not be responsible for displaying error messages. You would have another function that handled displaying error messages.

In your example, you don't need to return anything since the problem is being handled, but in real world applications you would have your logic divided up by functionality, and you would use try-catches, as well as returns from functions, to help make your program more robust.

This post has been edited by groovicus: 06 January 2011 - 10:53 AM

"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#5 User is offline   KermitD 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 81
  • Joined: 28-April 05
  • Location:Stelle, Illinois

Posted 10 January 2011 - 12:50 PM

Thank you Groovicus. I was over-complicating something that is really pretty straight forward.
I really appreciate you taking the time to straighten me out.
KermitD
Choices have consequences.

#6 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,605
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 10 January 2011 - 02:41 PM

Quote

I was over-complicating something that is really pretty straight forward.

Story of my life right there...
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users