21
VB .Net - Exceptions Copyright © Martin Schray 1997- 2003

VB.Net - Exceptions Copyright © Martin Schray 1997- 2003

Embed Size (px)

Citation preview

Page 1: VB.Net - Exceptions Copyright © Martin Schray 1997- 2003

VB .Net - Exceptions

Copyright © Martin Schray 1997- 2003

Page 2: 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)

Page 3: VB.Net - Exceptions Copyright © Martin Schray 1997- 2003

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

Page 4: VB.Net - Exceptions Copyright © Martin Schray 1997- 2003

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

Page 5: VB.Net - Exceptions Copyright © Martin Schray 1997- 2003

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

Page 6: VB.Net - Exceptions Copyright © Martin Schray 1997- 2003

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

Page 7: VB.Net - Exceptions Copyright © Martin Schray 1997- 2003

Try Catch Syntax

Try [ tryStatements ]

[ Catch [ exception [ As type ] ] [ When expression ] [ catchStatements ] ]

[ Exit Try ] ... [ Finally [ finallyStatements ] ] End Try

Page 8: VB.Net - Exceptions Copyright © Martin Schray 1997- 2003

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.

Page 9: VB.Net - Exceptions Copyright © Martin Schray 1997- 2003

Non-exception code example

int myInt = 0;

myInt = myInt/0; ‘ whoops division by zero!

Page 10: VB.Net - Exceptions Copyright © Martin Schray 1997- 2003

Exception technique example

int myInt = 0; Try

myInt= 1/0;

Catch diverr As DivideByZeroException

MsgBox("division by zero is not defined")

End Try

Page 11: VB.Net - Exceptions Copyright © Martin Schray 1997- 2003

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

Page 12: VB.Net - Exceptions Copyright © Martin Schray 1997- 2003

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

Page 13: VB.Net - Exceptions Copyright © Martin Schray 1997- 2003

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

Page 14: VB.Net - Exceptions Copyright © Martin Schray 1997- 2003

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

Page 15: VB.Net - Exceptions Copyright © Martin Schray 1997- 2003

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

Page 16: VB.Net - Exceptions Copyright © Martin Schray 1997- 2003

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 ()

Page 17: VB.Net - Exceptions Copyright © Martin Schray 1997- 2003

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

Page 18: VB.Net - Exceptions Copyright © Martin Schray 1997- 2003

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

Page 19: VB.Net - Exceptions Copyright © Martin Schray 1997- 2003

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

….

Page 20: VB.Net - Exceptions Copyright © Martin Schray 1997- 2003

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)

Page 21: VB.Net - Exceptions Copyright © Martin Schray 1997- 2003

Stack traces

• Typically include the line number of the calls as part of the stack trace