Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Visual Basic programming


  • Please log in to reply
5 replies to this topic

#1 KermitD

KermitD

  • Members
  • 81 posts
  • OFFLINE
  •  
  • Location:Stelle, Illinois
  • Local time:07:55 PM

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.

BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:55 PM

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.

Edited by groovicus, 06 January 2011 - 10:52 AM.


#3 KermitD

KermitD
  • Topic Starter

  • Members
  • 81 posts
  • OFFLINE
  •  
  • Location:Stelle, Illinois
  • Local time:07:55 PM

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

Edited by KermitD, 06 January 2011 - 10:20 AM.

KermitD
Choices have consequences.

#4 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:55 PM

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.

Edited by groovicus, 06 January 2011 - 10:53 AM.


#5 KermitD

KermitD
  • Topic Starter

  • Members
  • 81 posts
  • OFFLINE
  •  
  • Location:Stelle, Illinois
  • Local time:07:55 PM

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 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:55 PM

Posted 10 January 2011 - 02:41 PM

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

Story of my life right there...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users