• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[TUTORIAL] Handling Errors in VB.NET
#1
Handling Errors in VB.NET
Errors are easy to get and sometimes tedious to fix

Errors; they piss you off, they piss ME off. You're eventually going to get that error that you think is going to be impossible to fix. Let me tell you this: You're wrong. VB.NET has a statement called the Try...Catch...Finally statement that is made specifically for error handling. I've devoted this post specifically for Error Handling instead of adding this to the Statements section because I feel as if this is one of the most important statements that you should learn to use.

Let's go over it

Try...Catch...Finally

"Provides a way to handle some or all possible errors that may occur in a given block of code, while still running code."
This is how one of these statements would look:
Try
[ tryStatements ]
[ Catch [ exception [ As type ] ] [ When expression ]
[ catchStatements ] ]
[ Exit Try ]
...
[ Finally
[ finallyStatements ] ]
End Try


The break down
(Courtesy of MSDN)
  • tryStatements
    • This is optional but it's pointless if you do not include these. This is the code where you think an error will occur. There can be multiple statements within for checking.
  • Catch
    • Optional. Multiple Catch blocks permitted. If an exception occurs while processing the Try block, each Catch statement is examined in textual order to determine if it handles the exception. Exception represents the exception that has been thrown.
  • exception
    • Optional. Any variable name. The initial value of exception is the value of the thrown error. Used with Catch to specify the error caught.
  • type
    • Optional. Specifies the type of class filter. If the value of exception is of the type specified by type or of a derived type, the identifier becomes bound to the exception object.
  • When
    • Optional. A Catch statement with a When clause will only catch exceptions when expression evaluates to True. A When clause is only applied after checking the type of the exception, and expression may refer to the identifier representing the exception.
  • expression
    • Optional. Must be implicitly convertible to Boolean. Any expression that describes a generic filter. Typically used to filter by error number. Used with When keyword to specify circumstances under which the error is caught.
  • catchStatements
    • Optional. Statement(s) to handle errors occurring in the associated Try block. Can be a compound statement.
  • ExitTry
    • Optional. Keyword that breaks out of the Try...Catch...Finally structure. Execution resumes with the Finally block if present, otherwise with the code immediately following the End Try statement. Not allowed in Finally blocks.
  • Finally
    • Optional. A Finally block is always executed when execution leaves any part of the Try statement.
  • finallyStatements
    • Optional. Statement(s) that are executed after all other error processing has occurred.
  • End Try
    • Terminates the Try...Catch...Finally structure.

Credits:
SomeWhiteGuy - Original Post
[Image: uCZ3X.gif]
  Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  TUTORIAL CHEAT ENGINE for mw3 [HARD] Tony. 5 1,738 10-24-2013, 15:22
Last Post: Tomsen1410
  How do you guys check for errors when scripting? akillj 6 1,471 06-15-2013, 10:48
Last Post: Yamato
Question Tutorial addon! [HARD] Tony. 2 1,229 04-30-2013, 13:55
Last Post: [HARD] Tony.
  [TUTORIAL] Various Statements KrypTiK 2 994 01-07-2013, 21:00
Last Post: kokole
  Help Game Programming Tutorial Ich1994 8 1,796 01-01-2013, 21:21
Last Post: Ich1994
  [TUTORIAL] C# Introduction KrypTiK 4 1,239 12-30-2012, 10:06
Last Post: KrypTiK
  [TUTORIAL] Basic Syntax KrypTiK 0 807 12-29-2012, 20:33
Last Post: KrypTiK
  [TUTORIAL] Getting to know your way around the IDE KrypTiK 0 751 12-29-2012, 20:23
Last Post: KrypTiK
  [TUTORIAL] Introduction Into VB.NET KrypTiK 0 777 12-29-2012, 20:15
Last Post: KrypTiK
  Help C# Tutorial on how to reset a string DidUknowiPwn 7 2,262 10-11-2012, 15:04
Last Post: DidUknowiPwn

Forum Jump:


Users browsing this thread: 1 Guest(s)