chapter 12: advanced topics: exception handling visual basic.net programming: from problem analysis...
TRANSCRIPT
Chapter 12: Advanced Topics: Exception Handling
Visual Basic .NET Programming:
From Problem Analysis to Program Design
Visual Basic .NET Programming: From Problem Analysis to Program Design 2
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
Visual Basic .NET Programming: From Problem Analysis to Program Design 3
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
Visual Basic .NET Programming: From Problem Analysis to Program Design 4
Causing an Exception
• Create deliberate error
• Exception is thrown
– Message dialog indicates that code did not deal with exception
– Execution is interrupted
Visual Basic .NET Programming: From Problem Analysis to Program Design 5
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
Visual Basic .NET Programming: From Problem Analysis to Program Design 6
Visual Basic .NET Programming: From Problem Analysis to Program Design 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
Visual Basic .NET Programming: From Problem Analysis to Program Design 8
Exception-Handling Syntax (continued)
• Exception-related keywords:
– Try
– Catch
– Finally
– End Try
– Throw
Visual Basic .NET Programming: From Problem Analysis to Program Design 9
Visual Basic .NET Programming: From Problem Analysis to Program Design 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
Visual Basic .NET Programming: From Problem Analysis to Program Design 11
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
Visual Basic .NET Programming: From Problem Analysis to Program Design 12
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)
Visual Basic .NET Programming: From Problem Analysis to Program Design 13
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
Visual Basic .NET Programming: From Problem Analysis to Program Design 14
Exploring the Supplied Exception Classes
• Exception class
– Base class of all exceptions
– Contains:
• Constructor
• Message property
• ApplicationException
– Superclass for user-defined exceptions
Visual Basic .NET Programming: From Problem Analysis to Program Design 15
Exploring the Supplied Exception Classes (continued)
• SystemException
– Base class for exceptions used by VB .NET
• Properties can also throw exceptions
Visual Basic .NET Programming: From Problem Analysis to Program Design 16
Visual Basic .NET Programming: From Problem Analysis to Program Design 17
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
Visual Basic .NET Programming: From Problem Analysis to Program Design 18
Visual Basic .NET Programming: From Problem Analysis to Program Design 19
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
Visual Basic .NET Programming: From Problem Analysis to Program Design 20
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
Visual Basic .NET Programming: From Problem Analysis to Program Design 21
Writing User-Defined Exception Classes
• Essentially same as writing any class definition
• Include:
– Attribute definitions
– Accessor methods
– Constructors
• Naming convention:
– Name includes word “Exception”
Visual Basic .NET Programming: From Problem Analysis to Program Design 22
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
Visual Basic .NET Programming: From Problem Analysis to Program Design 23
Example 12-7: InvalidPhoneTypeException
Class Definition (excerpt)
1. Public Class InvalidPhoneTypeException
2. Inherits ApplicationException
3. Private phoneTypeSubmitted As String
…
11. End Class
Visual Basic .NET Programming: From Problem Analysis to Program Design 24
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
Visual Basic .NET Programming: From Problem Analysis to Program Design 25
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
Visual Basic .NET Programming: From Problem Analysis to Program Design 26
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
Visual Basic .NET Programming: From Problem Analysis to Program Design 27
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
Visual Basic .NET Programming: From Problem Analysis to Program Design 28
Visual Basic .NET Programming: From Problem Analysis to Program Design 29
Visual Basic .NET Programming: From Problem Analysis to Program Design 30
Visual Basic .NET Programming: From Problem Analysis to Program Design 31
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
Visual Basic .NET Programming: From Problem Analysis to Program Design 32
Visual Basic .NET Programming: From Problem Analysis to Program Design 33
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
Visual Basic .NET Programming: From Problem Analysis to Program Design 34
Creating a Watch Window (continued)
• To display Watch Window:
– Click Debug on the menu bar
– Point to Windows
– Point to Watch
– Click Watch 1
Visual Basic .NET Programming: From Problem Analysis to Program Design 35
Visual Basic .NET Programming: From Problem Analysis to Program Design 36
Visual Basic .NET Programming: From Problem Analysis to Program Design 37
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
Visual Basic .NET Programming: From Problem Analysis to Program Design 38
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
Visual Basic .NET Programming: From Problem Analysis to Program Design 39
Visual Basic .NET Programming: From Problem Analysis to Program Design 40
Visual Basic .NET Programming: From Problem Analysis to Program Design 41
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
Visual Basic .NET Programming: From Problem Analysis to Program Design 42
Programming Example: Data Validation with User-DefinedException Classes (continued)
• Problem analysis and algorithm design:
– Server module consists of a single method named ComputeWithHolding
– Method throws exceptions:
• InvalidMaritalStatusException
• InvalidWagesException
Visual Basic .NET Programming: From Problem Analysis to Program Design 43
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
Visual Basic .NET Programming: From Problem Analysis to Program Design 44
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
Visual Basic .NET Programming: From Problem Analysis to Program Design 45
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