460 likes | 739 Views
Chapter 12: Advanced Topics: Exception Handling. Visual Basic .NET Programming: From Problem Analysis to Program Design. Objectives. Examine the VB .NET exception-handling model Explore the supplied Exception classes Write user-defined Exception classes Add data validation code to methods
E N D
Chapter 12: Advanced Topics: Exception Handling Visual Basic .NET Programming: From Problem Analysis to Program Design
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
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
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
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
Visual Basic .NET Programming: From Problem Analysis to Program Design
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
Exception-Handling Syntax (continued) • Exception-related keywords: • Try • Catch • Finally • End Try • Throw Visual Basic .NET Programming: From Problem Analysis to Program Design
Visual Basic .NET Programming: From Problem Analysis to Program Design
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
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
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
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
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
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
Visual Basic .NET Programming: From Problem Analysis to Program Design
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
Visual Basic .NET Programming: From Problem Analysis to Program Design
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
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
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
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
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
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
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
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
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
Visual Basic .NET Programming: From Problem Analysis to Program Design
Visual Basic .NET Programming: From Problem Analysis to Program Design
Visual Basic .NET Programming: From Problem Analysis to Program Design
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
Visual Basic .NET Programming: From Problem Analysis to Program Design
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
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
Visual Basic .NET Programming: From Problem Analysis to Program Design
Visual Basic .NET Programming: From Problem Analysis to Program Design
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
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
Visual Basic .NET Programming: From Problem Analysis to Program Design
Visual Basic .NET Programming: From Problem Analysis to Program Design
Programming Example: Data Validation with User-DefinedException 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
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
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 • Tryblock • Code that may throw an exception should be placed inside Visual Basic .NET Programming: From Problem Analysis to Program Design
Summary (continued) • Throw keyword • Used by server to send exception to client • Catch block • Client code to process exception • Finallyblock • Optional • If included will execute regardless of whether exception is caught Visual Basic .NET Programming: From Problem Analysis to Program Design
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 Visual Basic .NET Programming: From Problem Analysis to Program Design