SlideShare uma empresa Scribd logo
1 de 54
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events
Chapter Objectives ,[object Object],[object Object],[object Object],[object Object]
Chapter Objectives (continued) ,[object Object],[object Object],[object Object]
Exception ,[object Object],[object Object],[object Object],[object Object],[object Object]
Handling Exception within a Program ,[object Object],[object Object],[object Object]
Java’s Mechanism of Exception Handling ,[object Object],[object Object],[object Object]
Java’s Mechanism of Exception Handling (continued) ,[object Object],[object Object],[object Object]
try / catch / finally  Block ,[object Object],[object Object],[object Object],[object Object]
try / catch / finally  Block (continued) ,[object Object],[object Object],[object Object]
try / catch / finally  Block (continued)
try / catch / finally  Block (continued) ,[object Object],[object Object],[object Object]
try / catch / finally  Block (continued) ,[object Object],[object Object]
Order of  catch  Blocks ,[object Object],[object Object],[object Object]
Order of  catch  Blocks (continued) ,[object Object],[object Object]
Java Exception Hierarchy
Java Exception Hierarchy (continued)
Java Exception Hierarchy (continued)
Java Exception Hierarchy (continued)
Java Exception Hierarchy (continued)
Java’s Exception Class ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
The  class  Exception  and its Constructors
Java Exception Classes
Java Exception Classes (continued)
Java Exception Classes (continued)
Java Exception Classes (continued)
Java Exception Classes (continued)
Java Exception Classes (continued)
Java Exception Classes (continued)
Java Exception Classes (continued)
Checked Exceptions ,[object Object],[object Object],[object Object]
Unchecked Exceptions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Exceptions Example Code ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
The  class   Exception  and  the Operator  instanceof ,[object Object],[object Object],[object Object]
The  class   Exception  and  the Operator  instanceof try   { System.out.print("Line 4: Enter the " + "dividend: ");  dividend = console.nextInt();  System.out.println();  System.out.print("Line 7: Enter the "  + "divisor: ");  divisor = console.nextInt();  System.out.println(); quotient = dividend / divisor;  System.out.println("Line 11: Quotient = " + quotient);  } catch  (Exception eRef) { if  (eRef  instanceof  ArithmeticException) System.out.println("Line 14: Exception " + eRef.toString());  else if  (eRef  instanceof  InputMismatchException) System.out.println("Line 16: Exception " + eRef.toString());  }
Rethrowing and Throwing an Exception ,[object Object],[object Object],[object Object],[object Object]
Rethrowing and Throwing an Exception (continued) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Rethrowing and Throwing an Exception (continued) import  java.util.*; public class  RethrowExceptionExmp1 { static  Scanner console =  new  Scanner(System.in); public static void  main(String[] args) { int  number;  try   { number = getNumber();  System.out.println("Line 5: number = " + number);  } catch  (InputMismatchException imeRef) { System.out.println("Line 7: Exception " + imeRef.toString()); } } }
Rethrowing and Throwing an Exception (continued) public static int  getNumber() throws  InputMismatchException  { int  num; try { System.out.print("Line 11: Enter an “  + "integer: ");  num = console.nextInt();  System.out.println();  return  num;  } catch  (InputMismatchException imeRef) { throw  imeRef;  } } }
The Method  printStackTrace   ,[object Object]
The Method  printStackTrace  (continued)   import  java.io.*; public class  PrintStackTraceExample1 { public static void  main(String[] args) { try { methodA(); } catch  (Exception e) { System.out.println(e.toString()  + " caught in main"); e.printStackTrace(); } }
The Method  printStackTrace  (continued)   public static void  methodA()  throws  Exception { methodB(); } public static void  methodB()  throws  Exception { methodC(); } public static void  methodC()  throws  Exception { throw   new  Exception("Exception generated  " +  " in method C"); } }
The Method  printStackTrace  (continued)   ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Exception-Handling Techniques ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Creating Your Own Exception Classes ,[object Object],[object Object],[object Object]
Creating Your Own Exception Classes (continued) public class  MyDivisionByZeroException  extends  Exception { public  MyDivisionByZeroException() { super ("Cannot divide by zero"); } public  MyDivisionByZeroException(String strMessage) { super (strMessage); } }
Event Handling ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Event Handling (continued) ,[object Object],[object Object],[object Object],[object Object]
Registering Listeners ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Event Handling (continued)
Event Handling (continued)
Event Handling (continued)
Programming Example: Calculator
Chapter Summary ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Chapter Summary (continued) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

Mais conteúdo relacionado

Mais procurados

Урок 7 для 6 класу - Багатошарові зображення, розміщення об’єктів у шарах
Урок 7 для 6 класу - Багатошарові зображення, розміщення об’єктів у шарахУрок 7 для 6 класу - Багатошарові зображення, розміщення об’єктів у шарах
Урок 7 для 6 класу - Багатошарові зображення, розміщення об’єктів у шарахVsimPPT
 
java-thread
java-threadjava-thread
java-threadbabu4b4u
 
Collections Framework
Collections FrameworkCollections Framework
Collections FrameworkSunil OS
 
เทคนิคปฏิบัติการทางเคมี M4
เทคนิคปฏิบัติการทางเคมี M4เทคนิคปฏิบัติการทางเคมี M4
เทคนิคปฏิบัติการทางเคมี M4พัน พัน
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaPratik Soares
 
บทที่3การสืบพันธุ์เจริญเติบโตพืชดอก
บทที่3การสืบพันธุ์เจริญเติบโตพืชดอกบทที่3การสืบพันธุ์เจริญเติบโตพืชดอก
บทที่3การสืบพันธุ์เจริญเติบโตพืชดอกWichai Likitponrak
 
structure and function of the stem
structure and function of the stemstructure and function of the stem
structure and function of the stemThanyamon Chat.
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with JavaJussi Pohjolainen
 
Java Garbage Collection - How it works
Java Garbage Collection - How it worksJava Garbage Collection - How it works
Java Garbage Collection - How it worksMindfire Solutions
 
ข้อสอบกระบวนวิชา Psy1001 (pc 103) จิตวิทยาทั่วไป
ข้อสอบกระบวนวิชา Psy1001 (pc 103) จิตวิทยาทั่วไปข้อสอบกระบวนวิชา Psy1001 (pc 103) จิตวิทยาทั่วไป
ข้อสอบกระบวนวิชา Psy1001 (pc 103) จิตวิทยาทั่วไปPijak Insawang
 
this keyword in Java.pptx
this keyword in Java.pptxthis keyword in Java.pptx
this keyword in Java.pptxParvizMirzayev2
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handlingteach4uin
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File HandlingSunil OS
 

Mais procurados (20)

Урок 7 для 6 класу - Багатошарові зображення, розміщення об’єктів у шарах
Урок 7 для 6 класу - Багатошарові зображення, розміщення об’єктів у шарахУрок 7 для 6 класу - Багатошарові зображення, розміщення об’єктів у шарах
Урок 7 для 6 класу - Багатошарові зображення, розміщення об’єктів у шарах
 
java-thread
java-threadjava-thread
java-thread
 
Collections Framework
Collections FrameworkCollections Framework
Collections Framework
 
เทคนิคปฏิบัติการทางเคมี M4
เทคนิคปฏิบัติการทางเคมี M4เทคนิคปฏิบัติการทางเคมี M4
เทคนิคปฏิบัติการทางเคมี M4
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
บทที่3การสืบพันธุ์เจริญเติบโตพืชดอก
บทที่3การสืบพันธุ์เจริญเติบโตพืชดอกบทที่3การสืบพันธุ์เจริญเติบโตพืชดอก
บทที่3การสืบพันธุ์เจริญเติบโตพืชดอก
 
презентація фільтрація даних
презентація фільтрація данихпрезентація фільтрація даних
презентація фільтрація даних
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 
structure and function of the stem
structure and function of the stemstructure and function of the stem
structure and function of the stem
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Exceptions handling notes in JAVA
Exceptions handling notes in JAVAExceptions handling notes in JAVA
Exceptions handling notes in JAVA
 
3 movement plan
3 movement plan3 movement plan
3 movement plan
 
Java Garbage Collection - How it works
Java Garbage Collection - How it worksJava Garbage Collection - How it works
Java Garbage Collection - How it works
 
ข้อสอบกระบวนวิชา Psy1001 (pc 103) จิตวิทยาทั่วไป
ข้อสอบกระบวนวิชา Psy1001 (pc 103) จิตวิทยาทั่วไปข้อสอบกระบวนวิชา Psy1001 (pc 103) จิตวิทยาทั่วไป
ข้อสอบกระบวนวิชา Psy1001 (pc 103) จิตวิทยาทั่วไป
 
this keyword in Java.pptx
this keyword in Java.pptxthis keyword in Java.pptx
this keyword in Java.pptx
 
Files in java
Files in javaFiles in java
Files in java
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handling
 
Exception handling in java
Exception handling  in javaException handling  in java
Exception handling in java
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
 

Destaque

היחס לאויב
היחס לאויבהיחס לאויב
היחס לאויבtarbut_il
 
биология 9 класс
биология 9 классбиология 9 класс
биология 9 классDENGALKRAP
 
98年 中小企業出口額-按行業別分-詹翔霖教授
98年 中小企業出口額-按行業別分-詹翔霖教授98年 中小企業出口額-按行業別分-詹翔霖教授
98年 中小企業出口額-按行業別分-詹翔霖教授文化大學
 
守候日記 980303 網路版
守候日記 980303 網路版守候日記 980303 網路版
守候日記 980303 網路版guestabef659
 
981管理學學生商訪講解(20091011)
981管理學學生商訪講解(20091011)981管理學學生商訪講解(20091011)
981管理學學生商訪講解(20091011)祐承 鄭
 
96th all japan-library_conference(20100917)
96th all japan-library_conference(20100917)96th all japan-library_conference(20100917)
96th all japan-library_conference(20100917)真 岡本
 
971023 [討論]友善單車城市藍圖
971023 [討論]友善單車城市藍圖971023 [討論]友善單車城市藍圖
971023 [討論]友善單車城市藍圖lifan
 
Arte y Nuevas Tecnologias ¿Nuevos Educadores?
Arte y Nuevas Tecnologias ¿Nuevos Educadores?Arte y Nuevas Tecnologias ¿Nuevos Educadores?
Arte y Nuevas Tecnologias ¿Nuevos Educadores?claudiamilenarangelsanchez
 
Недорубова Т.И. - 9 класс. Теория вероятности
Недорубова Т.И. - 9 класс. Теория вероятностиНедорубова Т.И. - 9 класс. Теория вероятности
Недорубова Т.И. - 9 класс. Теория вероятностиАлексей Арешев
 
970929 [討論]台灣路況對單車影響
970929 [討論]台灣路況對單車影響970929 [討論]台灣路況對單車影響
970929 [討論]台灣路況對單車影響lifan
 
9912 mt520healingasmissions
9912 mt520healingasmissions9912 mt520healingasmissions
9912 mt520healingasmissionsLaura Sun
 
9722609 Betty
9722609 Betty9722609 Betty
9722609 Bettykdlsldn
 
版面設計980413
版面設計980413版面設計980413
版面設計980413Louisa Li
 
9486 Invitopalazzettobianco
9486 Invitopalazzettobianco9486 Invitopalazzettobianco
9486 Invitopalazzettobiancolilith83
 
971008 [討論]單車設計階段報告
971008 [討論]單車設計階段報告971008 [討論]單車設計階段報告
971008 [討論]單車設計階段報告lifan
 
9789740331469
97897403314699789740331469
9789740331469CUPress
 

Destaque (20)

היחס לאויב
היחס לאויבהיחס לאויב
היחס לאויב
 
9582 Zadanie 2
9582 Zadanie 29582 Zadanie 2
9582 Zadanie 2
 
биология 9 класс
биология 9 классбиология 9 класс
биология 9 класс
 
98年 中小企業出口額-按行業別分-詹翔霖教授
98年 中小企業出口額-按行業別分-詹翔霖教授98年 中小企業出口額-按行業別分-詹翔霖教授
98年 中小企業出口額-按行業別分-詹翔霖教授
 
守候日記 980303 網路版
守候日記 980303 網路版守候日記 980303 網路版
守候日記 980303 網路版
 
957.Inst.Andino
957.Inst.Andino957.Inst.Andino
957.Inst.Andino
 
981管理學學生商訪講解(20091011)
981管理學學生商訪講解(20091011)981管理學學生商訪講解(20091011)
981管理學學生商訪講解(20091011)
 
96th all japan-library_conference(20100917)
96th all japan-library_conference(20100917)96th all japan-library_conference(20100917)
96th all japan-library_conference(20100917)
 
971023 [討論]友善單車城市藍圖
971023 [討論]友善單車城市藍圖971023 [討論]友善單車城市藍圖
971023 [討論]友善單車城市藍圖
 
Arte y Nuevas Tecnologias ¿Nuevos Educadores?
Arte y Nuevas Tecnologias ¿Nuevos Educadores?Arte y Nuevas Tecnologias ¿Nuevos Educadores?
Arte y Nuevas Tecnologias ¿Nuevos Educadores?
 
Недорубова Т.И. - 9 класс. Теория вероятности
Недорубова Т.И. - 9 класс. Теория вероятностиНедорубова Т.И. - 9 класс. Теория вероятности
Недорубова Т.И. - 9 класс. Теория вероятности
 
970929 [討論]台灣路況對單車影響
970929 [討論]台灣路況對單車影響970929 [討論]台灣路況對單車影響
970929 [討論]台灣路況對單車影響
 
9912 mt520healingasmissions
9912 mt520healingasmissions9912 mt520healingasmissions
9912 mt520healingasmissions
 
989 781-00-draehte boegen
989 781-00-draehte boegen989 781-00-draehte boegen
989 781-00-draehte boegen
 
9722609 Betty
9722609 Betty9722609 Betty
9722609 Betty
 
版面設計980413
版面設計980413版面設計980413
版面設計980413
 
9486 Invitopalazzettobianco
9486 Invitopalazzettobianco9486 Invitopalazzettobianco
9486 Invitopalazzettobianco
 
971008 [討論]單車設計階段報告
971008 [討論]單車設計階段報告971008 [討論]單車設計階段報告
971008 [討論]單車設計階段報告
 
9789740331469
97897403314699789740331469
9789740331469
 
98 ilo
98 ilo98 ilo
98 ilo
 

Semelhante a 9781439035665 ppt ch11

130410107010 exception handling
130410107010 exception handling130410107010 exception handling
130410107010 exception handlingHemant Chetwani
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptionssaman Iftikhar
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threadsDevaKumari Vijay
 
Md07 exceptions&assertion
Md07 exceptions&assertionMd07 exceptions&assertion
Md07 exceptions&assertionRakesh Madugula
 
9781111530532 ppt ch11
9781111530532 ppt ch119781111530532 ppt ch11
9781111530532 ppt ch11Terry Yoast
 
8.Exception handling latest(MB).ppt .
8.Exception handling latest(MB).ppt      .8.Exception handling latest(MB).ppt      .
8.Exception handling latest(MB).ppt .happycocoman
 
Exception Handling.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptxprimevideos176
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVASURIT DATTA
 
MODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docxMODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docxVeerannaKotagi1
 
Exception Handling
Exception HandlingException Handling
Exception HandlingPRN USM
 
Java -Exception handlingunit-iv
Java -Exception handlingunit-ivJava -Exception handlingunit-iv
Java -Exception handlingunit-ivRubaNagarajan
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overviewBharath K
 
Java Exception.ppt
Java Exception.pptJava Exception.ppt
Java Exception.pptRanjithaM32
 

Semelhante a 9781439035665 ppt ch11 (20)

130410107010 exception handling
130410107010 exception handling130410107010 exception handling
130410107010 exception handling
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptions
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threads
 
Java unit3
Java unit3Java unit3
Java unit3
 
Md07 exceptions&assertion
Md07 exceptions&assertionMd07 exceptions&assertion
Md07 exceptions&assertion
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
9781111530532 ppt ch11
9781111530532 ppt ch119781111530532 ppt ch11
9781111530532 ppt ch11
 
8.Exception handling latest(MB).ppt .
8.Exception handling latest(MB).ppt      .8.Exception handling latest(MB).ppt      .
8.Exception handling latest(MB).ppt .
 
Exception Handling.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptx
 
Java exceptions
Java exceptionsJava exceptions
Java exceptions
 
Java unit 11
Java unit 11Java unit 11
Java unit 11
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
UNIT 2.pptx
UNIT 2.pptxUNIT 2.pptx
UNIT 2.pptx
 
MODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docxMODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docx
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Java -Exception handlingunit-iv
Java -Exception handlingunit-ivJava -Exception handlingunit-iv
Java -Exception handlingunit-iv
 
17 exceptions
17   exceptions17   exceptions
17 exceptions
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
 
Java Exception.ppt
Java Exception.pptJava Exception.ppt
Java Exception.ppt
 

Mais de Terry Yoast

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12Terry Yoast
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11Terry Yoast
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10Terry Yoast
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09Terry Yoast
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08Terry Yoast
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07Terry Yoast
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06Terry Yoast
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05Terry Yoast
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04Terry Yoast
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03Terry Yoast
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02Terry Yoast
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01Terry Yoast
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13Terry Yoast
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18Terry Yoast
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17Terry Yoast
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16Terry Yoast
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15Terry Yoast
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14Terry Yoast
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12Terry Yoast
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11Terry Yoast
 

Mais de Terry Yoast (20)

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11
 

9781439035665 ppt ch11

  • 1. Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10. try / catch / finally Block (continued)
  • 11.
  • 12.
  • 13.
  • 14.
  • 20.
  • 21. The class Exception and its Constructors
  • 23. Java Exception Classes (continued)
  • 24. Java Exception Classes (continued)
  • 25. Java Exception Classes (continued)
  • 26. Java Exception Classes (continued)
  • 27. Java Exception Classes (continued)
  • 28. Java Exception Classes (continued)
  • 29. Java Exception Classes (continued)
  • 30.
  • 31.
  • 32.
  • 33.
  • 34. The class Exception and the Operator instanceof try { System.out.print("Line 4: Enter the " + "dividend: "); dividend = console.nextInt(); System.out.println(); System.out.print("Line 7: Enter the " + "divisor: "); divisor = console.nextInt(); System.out.println(); quotient = dividend / divisor; System.out.println("Line 11: Quotient = " + quotient); } catch (Exception eRef) { if (eRef instanceof ArithmeticException) System.out.println("Line 14: Exception " + eRef.toString()); else if (eRef instanceof InputMismatchException) System.out.println("Line 16: Exception " + eRef.toString()); }
  • 35.
  • 36.
  • 37. Rethrowing and Throwing an Exception (continued) import java.util.*; public class RethrowExceptionExmp1 { static Scanner console = new Scanner(System.in); public static void main(String[] args) { int number; try { number = getNumber(); System.out.println("Line 5: number = " + number); } catch (InputMismatchException imeRef) { System.out.println("Line 7: Exception " + imeRef.toString()); } } }
  • 38. Rethrowing and Throwing an Exception (continued) public static int getNumber() throws InputMismatchException { int num; try { System.out.print("Line 11: Enter an “ + "integer: "); num = console.nextInt(); System.out.println(); return num; } catch (InputMismatchException imeRef) { throw imeRef; } } }
  • 39.
  • 40. The Method printStackTrace (continued) import java.io.*; public class PrintStackTraceExample1 { public static void main(String[] args) { try { methodA(); } catch (Exception e) { System.out.println(e.toString() + " caught in main"); e.printStackTrace(); } }
  • 41. The Method printStackTrace (continued) public static void methodA() throws Exception { methodB(); } public static void methodB() throws Exception { methodC(); } public static void methodC() throws Exception { throw new Exception("Exception generated " + " in method C"); } }
  • 42.
  • 43.
  • 44.
  • 45. Creating Your Own Exception Classes (continued) public class MyDivisionByZeroException extends Exception { public MyDivisionByZeroException() { super ("Cannot divide by zero"); } public MyDivisionByZeroException(String strMessage) { super (strMessage); } }
  • 46.
  • 47.
  • 48.
  • 53.
  • 54.