Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "VB.Net - Exceptions Copyright © Martin Schray 1997- 2003."— Presentation transcript:

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

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

3 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

4 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

5 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

6 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

7 Try Catch Syntax Try [ tryStatements ] [ Catch [ exception [ As type ] ] [ When expression ] [ catchStatements ] ] [ Exit Try ]... [ Finally [ finallyStatements ] ] End Try

8 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.

9 Non-exception code example int myInt = 0; myInt = myInt/0; ‘ whoops division by zero!

10 Exception technique example int myInt = 0; Try myInt= 1/0; Catch diverr As DivideByZeroException MsgBox("division by zero is not defined") End Try

11 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

12 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

13 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

14 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

15 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

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

17 Getting information about exceptions Exceptions have two properties that are useful for discovering information about an exception  StackTrace - prints the method call stack  Message - gets the text message associated with an exception

18 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

19 Making your own exception classes You will use inheritance Select the appropriate type of exception classes to inherit from (probably SystemException) System.ApplicationException System.SystemExceptionSystem.ApplicationExceptionSystem.SystemException ….

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

21 Stack traces Typically include the line number of the calls as part of the stack trace


Download ppt "VB.Net - Exceptions Copyright © Martin Schray 1997- 2003."

Similar presentations


Ads by Google