Upload
diane-bond
View
227
Download
1
Tags:
Embed Size (px)
Citation preview
VB .Net - Exceptions
Copyright © Martin Schray 1997- 2003
Exceptions
• During execution runtime errors can occur
• There are different levels of severity of runtime errors: warnings and errors
• Checking for all possible errors is a good practice, but not very practical (can even create code clutter)
Exceptions
• Exceptions provide a clean way to check for errors “without” cluttering code
• Exceptions provide a mechanism for signaling errors directly rather then relaying on checking return codes or state variables– Getting a return code back to a caller can be
tough– State vars don’t work well with threading
Exceptions
• Exceptions are a great way to provide information to developers using of a library (e.g. using Collection Namespace to store objects)
• Exceptions provide far more useful information then just a return code– can provide information about the methods
called up until the exception (call stack)– Useful text message describing the exception
Exceptions
• An exception is thrown when an “unexpected” error occurs
• An exception is caught by an code waiting for that exception or a group of exceptions to be thrown
Exception we seen so far...
• On some occasions our programs don’t seem to run or run only partially …hmmm..
• Looking at the VB .NET output window you see a runtime error message (e.g. null object or exceeded array bounds)
• This is an uncaught exception
Try Catch Syntax
Try [ tryStatements ]
[ Catch [ exception [ As type ] ] [ When expression ] [ catchStatements ] ]
[ Exit Try ] ... [ Finally [ finallyStatements ] ] End Try
Try Catch Syntax described
• Try – start try catch block
• Catch – each Catch statement is examined in order to
determine if it handles the exception (can be multiple catch statements)
• Finally – Optional. A Finally block is always executed when
execution leaves any part of the Try statement.
• End Try – Terminates the Try...Catch...Finally structure.
Non-exception code example
int myInt = 0;
myInt = myInt/0; ‘ whoops division by zero!
Exception technique example
int myInt = 0; Try
myInt= 1/0;
Catch diverr As DivideByZeroException
MsgBox("division by zero is not defined")
End Try
Exception technique example with finally
int myInt = 0; Try
myint = 1/0;
Catch diverr As DivideByZeroException
MsgBox("division by zero is not defined")
Finally
textbox1.text = “” ‘ make sure result is cleared
End Try
Explanation of Example
• try block encloses code that might generate an exception
• Each try block is immediately followed by one (or more) catch blocks
• Each catch block specifies the type of exception it can catch and an exception handler
• After the try completes successfully OR exception if thrown (whether it is caught or not) the finally is executed
What happens when a exception occurs
• When an exception is thrown, control leaves the try block
• The catch blocks are searched in order for “a match”
• If the thrown exception matches the parameter list for a catch that catch is executed
• Execution continues after last catch• Or• No catch matches and exception bubbles up
What happens when NO exception occurs
• The code in the try block is completely executed
• The catch blocks are skipped
• Execution resumes after the last catch block
Affects on execution
• If the code in a try block throws an exception will immediately leave the try and look for a matching catch
• If a catch block parameter matches the thrown exception execution continues in the exception handler and then after the last catch block
Exception and inheritance
• Various exceptions classes can be derived from a common superclass (System.Exception)
• If a catch is written to catch a superclass it will also catch of subclass of that superclass ()
Getting information about exceptions
• Exceptions have two properties that are useful for discovering information about an exceptionStackTrace - prints the method call stack Message - gets the text message associated
with an exception
How to throw an exception
• Find the appropriate exception class
• Make an object of that class
• throw it
Dim e As New System.DivideByZeroException()
Throw e
Making your own exception classes
• You will use inheritance
• Select the appropriate type of exception classes to inherit from (probably SystemException)
System.ApplicationException System.SystemException
….
Stack trace
• Arhhhh… what is it???• Usually there a tons of lines that seem to be
deep within the innards of VB .NET (makes sense we are using BCL class libraries)
• Look for the names of classes that you have written
• Read from the top down (to see the last line executed)
• Read from the bottom up (if you need to follow program flow ~ call stack)
Stack traces
• Typically include the line number of the calls as part of the stack trace