SlideShare a Scribd company logo
1 of 42
Chapter 8 Exceptions and Assertions
Objectives ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Definition ,[object Object],[object Object]
Not Catching Exceptions Error message for invalid input String inputStr; int   age; inputStr = JOptionPane.showInputDialog ( null ,  "Age:" ) ; age  = Integer.parseInt ( inputStr ) ; java.lang.NumberFormatException: ten at java.lang.Integer.parseInt(Integer.java:405) at java.lang.Integer.parseInt(Integer.java:454) at Ch8Sample1.main(Ch8Sample1.java:20)
Catching an Exception inputStr = JOptionPane.showInputDialog ( null ,  "Age:" ) ; try   { age = Integer.parseInt ( inputStr ) ; }   catch   ( NumberFormatException e ){ JOptionPane.showMessageDialog( null ,  "’"  + inputStr  +  "‘ is invalid"   +  "Please enter digits only" ); } try catch
try-catch  Control Flow
Getting Information ,[object Object],[object Object],[object Object],try   { . . . }   catch   ( NumberFormatException e ){ System.out.println ( e.getMessage ()) ; System.out.println ( e.printStackTrace ()) ; }
Multiple  catch  Blocks ,[object Object],try   { . . . age = Integer.parseInt ( inputStr ) ; . . . val = cal.get(id);  //cal is a GregorianCalendar . . . }   catch   ( NumberFormatException e ){ . . .  }  catch   ( ArrayIndexOutOfBoundsException e ){ . . .  }
Multiple  catch  Control Flow
The  finally  Block ,[object Object],[object Object]
try - catch - finally  Control Flow
Propagating Exceptions ,[object Object],[object Object],public   int  getAge ( )   throws  NumberFormatException  { . . . int  age = Integer.parseInt ( inputStr ) ; . . . return  age; }
Throwing Exceptions ,[object Object],[object Object],[object Object],public   void  doWork ( int   num )   throws  Exception  { . . . if  ( num != val )  throw new  Exception ( "Invalid val" ) ; . . . }
Exception Thrower ,[object Object],[object Object],[object Object],[object Object]
Types of Exception Throwers ,[object Object],[object Object],[object Object]
Sample Call Sequence
Exception Types ,[object Object],[object Object],[object Object]
Throwable Hierarchy ,[object Object]
Checked vs. Runtime ,[object Object],[object Object],[object Object],[object Object],[object Object]
Different Handling Rules ,[object Object],[object Object],[object Object],[object Object]
Handling Checked Exceptions
Handling Runtime Exceptions
Programmer-Defined Exceptions ,[object Object],[object Object],[object Object],[object Object]
Assertions ,[object Object],[object Object],[object Object],[object Object]
Sample Use #1 public double  deposit ( double  amount ) { double  oldBalance = balance; balance += amount; assert  balance > oldBalance; } public double  withdraw ( double  amount ) { double  oldBalance = balance; balance -= amount; assert  balance < oldBalance; }
Second Form ,[object Object],[object Object],[object Object]
Sample Use #2 public double  deposit ( double  amount ) { double  oldBalance = balance; balance += amount; assert  balance > oldBalance : &quot;Serious Error – balance did not &quot;  + &quot; increase after deposit&quot; ; }
Compiling Programs with Assertions ,[object Object],[object Object],[object Object]
Running Programs with Assertions ,[object Object],[object Object],[object Object]
Different Uses of Assertions ,[object Object],[object Object],[object Object]
Problem Statement ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Overall Plan ,[object Object],[object Object],[object Object],[object Object],[object Object]
Design Document This class maintains information on individual dorm residents. Specification for this class is provided to us. Resident The standard class for displaying messages. JOptionPane The user interface class for handling input routines.  InputHandler The given predefined class that maintains a list of Resident objects. Dorm The given predefined class that simulates the opening of a door. Door The top-level control object that manages other objects in the program. This is an instantiable main class. Ch8EntranceMonitor Purpose Class
Class Relationships (main class) Door JOptionPane Resident InputFrame Ch8EntranceMonitor Dorm
Development Steps ,[object Object],[object Object],[object Object],[object Object]
Step 1 Design ,[object Object],[object Object],[object Object]
Step 1 Code ,[object Object],[object Object],[object Object],Program source file is too big to list here. From now on, we ask you to view the source files using your Java IDE.
Step 1 Test ,[object Object],[object Object],[object Object],[object Object]
Step 2 Design ,[object Object],[object Object]
Step 2 Code ,[object Object],[object Object],[object Object],[object Object]
Step 2 Test ,[object Object],[object Object],[object Object],[object Object],[object Object]
Step 3: Finalize ,[object Object],[object Object],[object Object]

More Related Content

What's hot

Exception handling
Exception handlingException handling
Exception handling
Iblesoft
 
Effective Java Second Edition
Effective Java Second EditionEffective Java Second Edition
Effective Java Second Edition
losalamos
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsPython Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and Assertions
Ranel Padon
 
Chapter 4 flow control structures and arrays
Chapter 4 flow control structures and arraysChapter 4 flow control structures and arrays
Chapter 4 flow control structures and arrays
sshhzap
 

What's hot (20)

Exception handling
Exception handlingException handling
Exception handling
 
Templates and Exception Handling in C++
Templates and Exception Handling in C++Templates and Exception Handling in C++
Templates and Exception Handling in C++
 
Creating your own exception
Creating your own exceptionCreating your own exception
Creating your own exception
 
Exceptions
ExceptionsExceptions
Exceptions
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handling
 
Java unit3
Java unit3Java unit3
Java unit3
 
Effective Java Second Edition
Effective Java Second EditionEffective Java Second Edition
Effective Java Second Edition
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Exception Handling in the C++ Constructor
Exception Handling in the C++ ConstructorException Handling in the C++ Constructor
Exception Handling in the C++ Constructor
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsPython Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and Assertions
 
OCA Java SE 8 Exam Chapter 6 Exceptions
OCA Java SE 8 Exam Chapter 6 ExceptionsOCA Java SE 8 Exam Chapter 6 Exceptions
OCA Java SE 8 Exam Chapter 6 Exceptions
 
Exception handlingpdf
Exception handlingpdfException handlingpdf
Exception handlingpdf
 
Chapter 4 flow control structures and arrays
Chapter 4 flow control structures and arraysChapter 4 flow control structures and arrays
Chapter 4 flow control structures and arrays
 
Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Exception handling & logging in Java - Best Practices (Updated)
Exception handling & logging in Java - Best Practices (Updated)Exception handling & logging in Java - Best Practices (Updated)
Exception handling & logging in Java - Best Practices (Updated)
 

Similar to Java căn bản - Chapter8

Md07 exceptions&assertion
Md07 exceptions&assertionMd07 exceptions&assertion
Md07 exceptions&assertion
Rakesh Madugula
 
Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception Handling
AboMohammad10
 

Similar to Java căn bản - Chapter8 (20)

Md07 exceptions&assertion
Md07 exceptions&assertionMd07 exceptions&assertion
Md07 exceptions&assertion
 
Java -Exception handlingunit-iv
Java -Exception handlingunit-ivJava -Exception handlingunit-iv
Java -Exception handlingunit-iv
 
Nalinee java
Nalinee javaNalinee java
Nalinee java
 
Exception handling
Exception handlingException handling
Exception handling
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Java exceptions
Java exceptionsJava exceptions
Java exceptions
 
exception handling
exception handlingexception handling
exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptx
 
UNIT 2.pptx
UNIT 2.pptxUNIT 2.pptx
UNIT 2.pptx
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception Handling
 
SUBHASH.pptx
SUBHASH.pptxSUBHASH.pptx
SUBHASH.pptx
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#
 
Introduction of exception in vb.net
Introduction of exception in vb.netIntroduction of exception in vb.net
Introduction of exception in vb.net
 
UNIT III.ppt
UNIT III.pptUNIT III.ppt
UNIT III.ppt
 
UNIT III (2).ppt
UNIT III (2).pptUNIT III (2).ppt
UNIT III (2).ppt
 
Presentation1
Presentation1Presentation1
Presentation1
 
Java SE 11 Exception Handling
Java SE 11 Exception HandlingJava SE 11 Exception Handling
Java SE 11 Exception Handling
 
java exception.pptx
java exception.pptxjava exception.pptx
java exception.pptx
 

More from Vince Vo

Java căn bản - Chapter13
Java căn bản - Chapter13Java căn bản - Chapter13
Java căn bản - Chapter13
Vince Vo
 
Java căn bản - Chapter12
Java căn bản - Chapter12Java căn bản - Chapter12
Java căn bản - Chapter12
Vince Vo
 
Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10
Vince Vo
 
Java căn bản - Chapter9
Java căn bản - Chapter9Java căn bản - Chapter9
Java căn bản - Chapter9
Vince Vo
 
Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7
Vince Vo
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
Vince Vo
 
Java căn bản - Chapter5
Java căn bản - Chapter5Java căn bản - Chapter5
Java căn bản - Chapter5
Vince Vo
 
Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4
Vince Vo
 
Java căn bản - Chapter3
Java căn bản - Chapter3Java căn bản - Chapter3
Java căn bản - Chapter3
Vince Vo
 
Java căn bản - Chapter2
Java căn bản - Chapter2Java căn bản - Chapter2
Java căn bản - Chapter2
Vince Vo
 
Java căn bản- Chapter1
Java  căn bản- Chapter1Java  căn bản- Chapter1
Java căn bản- Chapter1
Vince Vo
 
Hướng dẫn cài đặt Java
Hướng dẫn cài đặt JavaHướng dẫn cài đặt Java
Hướng dẫn cài đặt Java
Vince Vo
 

More from Vince Vo (20)

Java căn bản - Chapter13
Java căn bản - Chapter13Java căn bản - Chapter13
Java căn bản - Chapter13
 
Java căn bản - Chapter12
Java căn bản - Chapter12Java căn bản - Chapter12
Java căn bản - Chapter12
 
Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10
 
Java căn bản - Chapter9
Java căn bản - Chapter9Java căn bản - Chapter9
Java căn bản - Chapter9
 
Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
Java căn bản - Chapter5
Java căn bản - Chapter5Java căn bản - Chapter5
Java căn bản - Chapter5
 
Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4
 
Java căn bản - Chapter3
Java căn bản - Chapter3Java căn bản - Chapter3
Java căn bản - Chapter3
 
Java căn bản - Chapter2
Java căn bản - Chapter2Java căn bản - Chapter2
Java căn bản - Chapter2
 
Java căn bản- Chapter1
Java  căn bản- Chapter1Java  căn bản- Chapter1
Java căn bản- Chapter1
 
Hướng dẫn cài đặt Java
Hướng dẫn cài đặt JavaHướng dẫn cài đặt Java
Hướng dẫn cài đặt Java
 
Rama Ch14
Rama Ch14Rama Ch14
Rama Ch14
 
Rama Ch13
Rama Ch13Rama Ch13
Rama Ch13
 
Rama Ch12
Rama Ch12Rama Ch12
Rama Ch12
 
Rama Ch12
Rama Ch12Rama Ch12
Rama Ch12
 
Rama Ch11
Rama Ch11Rama Ch11
Rama Ch11
 
Rama Ch10
Rama Ch10Rama Ch10
Rama Ch10
 
Rama Ch8
Rama Ch8Rama Ch8
Rama Ch8
 
Rama Ch9
Rama Ch9Rama Ch9
Rama Ch9
 

Recently uploaded

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 

Recently uploaded (20)

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 

Java căn bản - Chapter8

  • 1. Chapter 8 Exceptions and Assertions
  • 2.
  • 3.
  • 4. Not Catching Exceptions Error message for invalid input String inputStr; int age; inputStr = JOptionPane.showInputDialog ( null , &quot;Age:&quot; ) ; age = Integer.parseInt ( inputStr ) ; java.lang.NumberFormatException: ten at java.lang.Integer.parseInt(Integer.java:405) at java.lang.Integer.parseInt(Integer.java:454) at Ch8Sample1.main(Ch8Sample1.java:20)
  • 5. Catching an Exception inputStr = JOptionPane.showInputDialog ( null , &quot;Age:&quot; ) ; try { age = Integer.parseInt ( inputStr ) ; } catch ( NumberFormatException e ){ JOptionPane.showMessageDialog( null , &quot;’&quot; + inputStr + &quot;‘ is invalid&quot; + &quot;Please enter digits only&quot; ); } try catch
  • 7.
  • 8.
  • 9. Multiple catch Control Flow
  • 10.
  • 11. try - catch - finally Control Flow
  • 12.
  • 13.
  • 14.
  • 15.
  • 17.
  • 18.
  • 19.
  • 20.
  • 23.
  • 24.
  • 25. Sample Use #1 public double deposit ( double amount ) { double oldBalance = balance; balance += amount; assert balance > oldBalance; } public double withdraw ( double amount ) { double oldBalance = balance; balance -= amount; assert balance < oldBalance; }
  • 26.
  • 27. Sample Use #2 public double deposit ( double amount ) { double oldBalance = balance; balance += amount; assert balance > oldBalance : &quot;Serious Error – balance did not &quot; + &quot; increase after deposit&quot; ; }
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33. Design Document This class maintains information on individual dorm residents. Specification for this class is provided to us. Resident The standard class for displaying messages. JOptionPane The user interface class for handling input routines. InputHandler The given predefined class that maintains a list of Resident objects. Dorm The given predefined class that simulates the opening of a door. Door The top-level control object that manages other objects in the program. This is an instantiable main class. Ch8EntranceMonitor Purpose Class
  • 34. Class Relationships (main class) Door JOptionPane Resident InputFrame Ch8EntranceMonitor Dorm
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.

Editor's Notes

  1. We can increase our programs’ reliability and robustness if we catch the exceptions ourselves using error recovery routines we develop. One way to do this is to wrap the statements that may throw an exception with the try-catch control statement.
  2. Consider the given example. What would happen if the user enters a value such as the text &apos;ten&apos; instead of 10? The parseInt method cannot convert such an input to an internal numerical format. This type of error is called an exception, and it will result in displaying an error message such as the one shown here. We say the parseInt method has thrown a NumberFormatException.
  3. This example shows a way to handle a thrown exception in our code, instead of letting the system handle it, as in the previous example. If any statement inside the try block throws an exception, then the statements in the matching catch block are executed. And the program continues the execution from the statement that follows this try-catch statement.
  4. This illustrates how the control flows when there is an exception and when there is no exception. In the case when no statements in the try block throw an exception, then the catch block is skipped and execution continues with the next statement following the try-catch statement. If any one of the statements throws an exception, the statements in the catch block are executed. Execution then continues to the statement following the try-catch statement, ignoring any remaining statements in the try block.
  5. In the previous example, we simply displayed a fixed error message. We can get display a more generic error message by using the getMessage or printStrackTrace methods. We will experiment with these methods in the lab.
  6. In this example, we see two statements in the try block that can potentially throw exceptions. The parseInt method throws a NumberFormatException while the get method throws an ArrayIndexOutOfBoundsException when the argument id is outside the range of valid values.
  7. Here&apos;s how the control flows when there are multiple catch blocks. In the case when no statements in the try block throw an exception, then all catch blocks are skipped and execution continues with the next statement following the try-catch statement. If any one of the statements throws an exception, the statements in the matching catch block are executed. Execution then continues to the statement following the try-catch statement, ignoring any remaining statements in the try block.
  8. The finally block is not used often in introductory level programs, but we will introduce them here for the sake of complete coverage of the topic. For example, suppose we open a communication channel from our Java program to a remote web server to exchange data. If the data exchange is successfully completed in the try block, then we close the communication channel and finish the operation. If the data exchange is interrupted for some reason, an exception is thrown and the operation is aborted. In this case also, we need to close the communication channel, because leaving the channel open by one application blocks other applications from using it. The code to close the communication channel should therefore be placed in the finally block.
  9. Here&apos;s how the control flows when there are multiple catch blocks and the finally block. In the case when no statements in the try block throw an exception, then all catch blocks are skipped, but the statements in the finally block are executed. Execution continues with the next statement following the try-catch statement. If any one of the statements throws an exception, the statements in the matching catch block are executed first and the statements in the finally block are executed next. Execution then continues to the statement following the try-catch statement, ignoring any remaining statements in the try block.
  10. Using the try-catch statement is the first way to handle the exceptions. The second way is to propagate the thrown exception back to the caller of the method. The method that includes the statement that calls our method must either catch it or propagate it back to their caller.
  11. It is possible to throw an exception from our own method.
  12. We say a method throws an exception directly when the method includes the throw statement. Otherwise, a method is throwing an exception indirectly.
  13. This illustration shows a sequence of method calls among the exception throwers. Method D throws an instance of Exception. The green arrows indicate the direction of calls. The red arrows show the reversing of call sequence, looking for a matching catcher. Method B is the catcher. The call sequence is traced by using a stack.
  14. The classes shown here are some of the more common classes in the Throwable class hierarchy. We will be seeing most of the Exception and its subclasses shown here in the later modules.
  15. Callers of a method that can throw a checked exception must include the try-catch statement in the method body or the throws clause in the header.
  16. It is optional for callers of a method that can throw runtime exceptions to include the try-catch statement in the method body or the throws clause in the header.
  17. We will be seeing examples of using assertions in the sample programs later.
  18. For this application, we are given two helper classes. In order to use the Dorm class, we must supply the Resident class whose specification is already set so it works correctly with the given Dorm class. The relationship between the Dorm and Resident is similar to the one between the DrawingBoard and the DrawableShape classes.
  19. There will be a total of six key classes in this application. We will be designing three classes: Ch8EntranceMonitor, Resident, and InputFrame.
  20. Most relationships follow the client-service pattern, except the one between the InputFrame and Ch8EntranceMonitor follows the supervisor-subordinate pattern.
  21. Please use your Java IDE to view the source files and run the program.
  22. .