Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 12: Advanced Topics: Exception Handling Visual Basic.NET Programming: From Problem Analysis to Program Design.

Similar presentations


Presentation on theme: "Chapter 12: Advanced Topics: Exception Handling Visual Basic.NET Programming: From Problem Analysis to Program Design."— Presentation transcript:

1 Chapter 12: Advanced Topics: Exception Handling Visual Basic.NET Programming: From Problem Analysis to Program Design

2 Visual Basic.NET Programming: From Problem Analysis to Program Design2 Objectives Examine the VB.NET exception-handling model Explore the supplied Exception classes Write user-defined Exception classes Add data validation code to methods Explore the debugging tool

3 Visual Basic.NET Programming: From Problem Analysis to Program Design3 Examining the VB.NET Exception-Handling Model Exception –Object instance of Framework Class Library (FCL)-supplied Exception class Or one of its subclasses –Used by VB.NET to notify you of: Errors Problems Other unusual conditions that may occur while your system is running

4 Visual Basic.NET Programming: From Problem Analysis to Program Design4 Causing an Exception Create deliberate error Exception is thrown –Message dialog indicates that code did not deal with exception –Execution is interrupted

5 Visual Basic.NET Programming: From Problem Analysis to Program Design5 Example 12-2: Invoking the String Insert Method with an Invalid Argument (excerpt) 4. Sub Main() 5. Dim s1 As String = “Hello Again” 6. Dim s2 As String = s1.Insert(16, “There “) 7. Console.WriteLine(s2) 8. End Sub

6 Visual Basic.NET Programming: From Problem Analysis to Program Design6

7 7 Exception-Handling Syntax When exceptions arise –Server method can create exception instance containing information about situation –Server then sends exception instance to invoking client –Client must be prepared to receive exception and take appropriate action

8 Visual Basic.NET Programming: From Problem Analysis to Program Design8 Exception-Handling Syntax (continued) Exception-related keywords: –Try –Catch –Finally –End Try –Throw

9 Visual Basic.NET Programming: From Problem Analysis to Program Design9

10 10 Exception-Handling Syntax (continued) Try block –Code that may invoke exception placed in Try block –Begins with keyword Try –Ends with keyword End Try Throw keyword –Used by server to pass exception to client

11 Visual Basic.NET Programming: From Problem Analysis to Program Design11 Exception-Handling Syntax (continued) Catch block –Client catches exception instance –Executes statements to deal with exception Finally block –Optional –Will execute regardless of whether exception is caught

12 Visual Basic.NET Programming: From Problem Analysis to Program Design12 Example 12-3: Invoking the Insert Method Using Try-Catch (excerpt) 4. Sub Main() 5. Dim s1 As String = "Hello Again" 6. Try 7. Dim s2 As String = s1.Insert(16, "There ") 8. Console.WriteLine(s2)

13 Visual Basic.NET Programming: From Problem Analysis to Program Design13 Example 12-3: Invoking the Insert Method Using Try-Catch (continued) 9. Catch e As Exception 10. Console.WriteLine("Exception was caught:") 11. Console.WriteLine(e.Message) 12. Finally 13. Console.WriteLine("Finally block is always executed.") 14. End Try 15. End Sub

14 Visual Basic.NET Programming: From Problem Analysis to Program Design14 Exploring the Supplied Exception Classes Exception class –Base class of all exceptions –Contains: Constructor Message property ApplicationException –Superclass for user-defined exceptions

15 Visual Basic.NET Programming: From Problem Analysis to Program Design15 Exploring the Supplied Exception Classes (continued) SystemException –Base class for exceptions used by VB.NET Properties can also throw exceptions

16 Visual Basic.NET Programming: From Problem Analysis to Program Design16

17 Visual Basic.NET Programming: From Problem Analysis to Program Design17 Example 12-4: Triggering an Exception from the CLR (excerpt) 3. Module Chapter12Example4 4. Sub Main() 5. Dim s1, s2 As String 6. s1 = “Hello Again” 7. s2 = s2.Insert(6, “There ”) 8. Console.WriteLine(s2) 9. End Sub 10. End Module

18 Visual Basic.NET Programming: From Problem Analysis to Program Design18

19 Visual Basic.NET Programming: From Problem Analysis to Program Design19 Example 12-5: Instantiating the Supplied Exception Classes (excerpt) Server module: 6. If b = 0 Then 7. e = New DivideByZeroException(“You cannot divide by zero”) 8. Throw e

20 Visual Basic.NET Programming: From Problem Analysis to Program Design20 Example 12-5: Instantiating the Supplied Exception Classes (excerpt) Client module: 6. Try … 11. Catch x As DivideByZeroException 12. Console.WriteLine(x.Message) 13. End Try

21 Visual Basic.NET Programming: From Problem Analysis to Program Design21 Writing User-Defined Exception Classes Essentially same as writing any class definition Include: –Attribute definitions –Accessor methods –Constructors Naming convention: –Name includes word “Exception”

22 Visual Basic.NET Programming: From Problem Analysis to Program Design22 Adding Data Validation Code to Methods Data validation code –Added to methods to ensure valid arguments are passed –Can throw exception when invalid data is used

23 Visual Basic.NET Programming: From Problem Analysis to Program Design23 Example 12-7: InvalidPhoneTypeException Class Definition (excerpt) 1. Public Class InvalidPhoneTypeException 2. Inherits ApplicationException 3. Private phoneTypeSubmitted As String … 11. End Class

24 Visual Basic.NET Programming: From Problem Analysis to Program Design24 Example 12-8: Phone Class Definition with Data Validation (excerpt) 15. Select Case aPhoneType 16. Case "Home", "home", "HOME" 17. typeOK = True 18. aPhoneType = "Home“ … 28. End Select

25 Visual Basic.NET Programming: From Problem Analysis to Program Design25 Example 12-8: Phone Class Definition with Data Validation (continued) 29. If typeOK Then 30. phoneType = aPhoneType 31. Else 32. e = New InvalidPhoneTypeException(aPhoneType, "Phone type must be Home, Mobile, Office, or Fax") 33. Throw e 34. End If

26 Visual Basic.NET Programming: From Problem Analysis to Program Design26 Exploring the Debugging Tool Debugger –Software tool –Provided to assist in finding errors that keep program from running as intended Watch window –View contents of variables and expressions

27 Visual Basic.NET Programming: From Problem Analysis to Program Design27 Setting a Breakpoint Breakpoint – Flag in program –Tells debugger to pause execution of program at specific line of code View and alter contents of variables Alter sequence of statement execution –By specifying next statement to be executed

28 Visual Basic.NET Programming: From Problem Analysis to Program Design28

29 Visual Basic.NET Programming: From Problem Analysis to Program Design29

30 Visual Basic.NET Programming: From Problem Analysis to Program Design30

31 Visual Basic.NET Programming: From Problem Analysis to Program Design31 Setting a Breakpoint (continued) Hit count –Number of times breakpoint is encountered before stopping –Default value is 1 Attach logical expression to breakpoint –Execution will stop if expression evaluates true

32 Visual Basic.NET Programming: From Problem Analysis to Program Design32

33 Visual Basic.NET Programming: From Problem Analysis to Program Design33 Creating a Watch Window Watch window –Display contents of variables while program is suspended at breakpoint –Available only when execution of program is suspended at breakpoint

34 Visual Basic.NET Programming: From Problem Analysis to Program Design34 Creating a Watch Window (continued) To display Watch Window: –Click Debug on the menu bar –Point to Windows –Point to Watch –Click Watch 1

35 Visual Basic.NET Programming: From Problem Analysis to Program Design35

36 Visual Basic.NET Programming: From Problem Analysis to Program Design36

37 Visual Basic.NET Programming: From Problem Analysis to Program Design37 Using the Step Feature Step –Execute one line of code at a time –Options: Step into Step over Step Into –Executes line of code and stops

38 Visual Basic.NET Programming: From Problem Analysis to Program Design38 Using the Step Feature (continued) Step Over –Executes next line of code –If code invokes a procedure All statements in procedure are executed Then debugger stops

39 Visual Basic.NET Programming: From Problem Analysis to Program Design39

40 Visual Basic.NET Programming: From Problem Analysis to Program Design40

41 Visual Basic.NET Programming: From Problem Analysis to Program Design41 Programming Example: Data Validation with User-Defined Exception Classes Extends Chapter 7 end-of-chapter programming example Demonstrates implementation of user-defined Exception classes used in data validation

42 Visual Basic.NET Programming: From Problem Analysis to Program Design42 Programming Example: Data Validation with User-Defined Exception Classes (continued) Problem analysis and algorithm design: –Server module consists of a single method named ComputeWithHolding –Method throws exceptions: InvalidMaritalStatusException InvalidWagesException

43 Visual Basic.NET Programming: From Problem Analysis to Program Design43 Summary Exceptions notify you of errors, problems, and other unusual conditions Exception class –Superclass for all exceptions Server methods can create exception instances –Containing information about the errors Try block –Code that may throw an exception should be placed inside

44 Visual Basic.NET Programming: From Problem Analysis to Program Design44 Summary (continued) Throw keyword –Used by server to send exception to client Catch block –Client code to process exception Finally block –Optional –If included will execute regardless of whether exception is caught

45 Visual Basic.NET Programming: From Problem Analysis to Program Design45 Summary (continued) Base class of all exceptions is the Exception class Visual Studio debugger –Software tool provided to assist in finding errors –Components: Breakpoints Watch window


Download ppt "Chapter 12: Advanced Topics: Exception Handling Visual Basic.NET Programming: From Problem Analysis to Program Design."

Similar presentations


Ads by Google