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

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

Pemrograman VisualMinggu …3… Page 1 MINGGU Ke Tiga Pemrograman Visual Pokok Bahasan: Class, Objects, Methods and Instance Variable Tujuan Instruksional.
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
C# Programming: From Problem Analysis to Program Design1 Debugging and Handling Exceptions C# Programming: From Problem Analysis to Program Design 3 rd.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling.
Chapter 11 Debugging and Handling Exceptions
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
Debugging Techniques1. 2 Introduction Bugs How to debug Using of debugger provided by the IDE Exception Handling Techniques.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Chapter 11: Implementing Inheritance and Association Visual Basic.NET Programming: From Problem Analysis to Program Design.
BIM313 – Advanced Programming Techniques Debugging 1.
Java Programming, 3e Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
Chapter 15 Strings String::Concat String::CompareTo, Equals, == If( string1 == S”Hello”) String1->Equals(S”Hello”) String1->CompareTo(S”Hello”) CompareTo.
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
Object-Oriented Application Development Using VB.NET 1 Chapter 7 Adding Responsibilities to Problem Domain Classes.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 10 Classes Continued
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
To define a class in Visual Basic.NET, you can follow this general procedure: 1. Add a class to the project. 2. Provide an appropriate file name for.
1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about.
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
Chapter 12: Exception Handling
Benefits of PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –List and explain the benefits of PL/SQL –List.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Debugging applications, using properties Jim Warren – COMPSCI 280 S Enterprise Software Development.
Enhancing User Interaction Through Programming
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
Chapter 8: Writing Graphical User Interfaces
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
Java Programming: Guided Learning with Early Objects
Object Oriented Software Development 8. Exceptions, testing and debugging.
Chapter 9: Writing Procedures Visual Basic.NET Programming: From Problem Analysis to Program Design.
Introduction to Exception Handling and Defensive Programming.
Controlling Execution Programming Right from the Start with Visual Basic.NET 1/e 8.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 8 Debugging, Creating Executable Files, and Distributing a Windows Application.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Object-Oriented Application Development Using VB.NET 1 Chapter 2 The Visual Studio.NET Development Environment.
Debug in Visual Studio Windows Development Fundamentals LESSON 2.5A.
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
15 Copyright © 2004, Oracle. All rights reserved. Debugging Triggers.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 2: The Visual Studio .NET Development Environment
Debugging and Handling Exceptions
Computer Programming I
Important terms Black-box testing White-box testing Regression testing
Important terms Black-box testing White-box testing Regression testing
Exception Handling Chapter 9.
CIS16 Application Development Programming with Visual Basic
Exception Handling Chapter 9 Edited by JJ.
Debugging and Handling Exceptions
Presentation 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 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

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

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

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

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

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 Design8 Exception-Handling Syntax (continued) Exception-related keywords: –Try –Catch –Finally –End Try –Throw

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

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

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)

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

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

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

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

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

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

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

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

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”

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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