More about Exceptions Java Interlude 4 Data Structures
More about Exceptions Java Interlude 4 Data Structures and Abstractions with Java, 4 e Frank Carrano © 2015 Pearson Education, Inc. , Upper Saddle River, NJ. All rights reserved.
Programmer-Defined Exception Classes • Define your own exception classes by extending existing exception classes − Existing superclass could be one in the Java Class Library or one of your own • Constructors in an exception subclass are the most important − Often the only methods you need to define © 2015 Pearson Education, Inc. , Upper Saddle River, NJ. All rights reserved.
Programmer-Defined Exception Classes LISTING JI 4 -1 The exception class Square. Root. Exception © 2015 Pearson Education, Inc. , Upper Saddle River, NJ. All rights reserved.
Programmer-Defined Exception Classes LISTING JI 4 -2 The class Our. Math and its static method square. Root © 2015 Pearson Education, Inc. , Upper Saddle River, NJ. All rights reserved.
Programmer-Defined Exception Classes LISTING JI 4 -2 The class Our. Math and its static method square. Root © 2015 Pearson Education, Inc. , Upper Saddle River, NJ. All rights reserved.
Programmer-Defined Exception Classes LISTING JI 4 -3 A driver for the class Our. Math © 2015 Pearson Education, Inc. , Upper Saddle River, NJ. All rights reserved.
Programmer-Defined Exception Classes LISTING JI 4 -3 A driver for the class Our. Math Output © 2015 Pearson Education, Inc. , Upper Saddle River, NJ. All rights reserved.
Programmer-Defined Exception Classes LISTING JI 4 -4 The class Joe. Math © 2015 Pearson Education, Inc. , Upper Saddle River, NJ. All rights reserved.
Programmer-Defined Exception Classes LISTING JI 4 -4 The class Joe. Math © 2015 Pearson Education, Inc. , Upper Saddle River, NJ. All rights reserved.
Programmer-Defined Exception Classes LISTING JI 4 -5 A driver for the class Joe. Math © 2015 Pearson Education, Inc. , Upper Saddle River, NJ. All rights reserved.
Programmer-Defined Exception Classes LISTING JI 4 -5 A driver for the class Joe. Math Output © 2015 Pearson Education, Inc. , Upper Saddle River, NJ. All rights reserved.
Inheritance and Exceptions Consider this superclass and subclass. We cannot override some. Method in a subclass and list additional checked exceptions in its throws clause © 2015 Pearson Education, Inc. , Upper Saddle River, NJ. All rights reserved.
Inheritance and Exceptions Only Exception 1 is caught. If the throws clause in Sub. Class was legal, we could call Sub. Class’s some. Method without catching Exception 2. © 2015 Pearson Education, Inc. , Upper Saddle River, NJ. All rights reserved.
Inheritance and Exceptions If Exception 2 extends Exception 1, the above is legal © 2015 Pearson Education, Inc. , Upper Saddle River, NJ. All rights reserved.
The finally Block This code shows the placement of the finally block © 2015 Pearson Education, Inc. , Upper Saddle River, NJ. All rights reserved.
The finally Block Whether an exception occurs or not, close. Refrigerator is called within the finally block. © 2015 Pearson Education, Inc. , Upper Saddle River, NJ. All rights reserved.
The finally Block LISTING JI 4 -6 A demonstration of a finally block © 2015 Pearson Education, Inc. , Upper Saddle River, NJ. All rights reserved.
The finally Block LISTING JI 4 -6 A demonstration of a finally block © 2015 Pearson Education, Inc. , Upper Saddle River, NJ. All rights reserved.
The finally Block LISTING JI 4 -6 A demonstration of a finally block Output © 2015 Pearson Education, Inc. , Upper Saddle River, NJ. All rights reserved.
End Java Interlude 4 © 2015 Pearson Education, Inc. , Upper Saddle River, NJ. All rights reserved.
- Slides: 20