Exceptions and Exception Handling
CS 240 – Advanced Programming Concepts
Exceptions Overview
2
Separation of Normal and Abnormal Processing Logic
public class FileReadingWithoutExceptions {�
public boolean readFile(String fileName, String fileContents) {� boolean success = true;�� File file = new File(filename);�� if(!file.exists()) {� success = false;� }�� if(success) {� fileContents = "<Insert code to read the
file here>";�� if(fileContents == "") {� success = false;� }� }�� return success;� }�}
public class FileReadingWithExceptions {
� public String readFile(String fileName) throws IOException {� File file = new File(fileName);� String fileContents = "<Insert code to read the
file here>";� return fileContents;� }�}
3
Partial Exception Class Hierarchy
4
Try / Catch Blocks
5
Syntax
try {
// Code that may throw an exception
} catch(SomeExceptionType ex) {
// Code to handle the exception
} catch(OtherExceptionType ex) {
// Code to handle the exception
}
Catching but not actually resolving an exception is sometimes called “swallowing” an exception and is a bad practice.
public class TryCatchExample {� public String readFile(String fileName) {� File file = new File(fileName);� String fileContents = "";�� try {� FileReader fr = new FileReader(file);� BufferedReader br = new BufferedReader(fr);�� String line;� while((line = br.readLine()) != null) {� fileContents += line;� fileContents += "\n";� }�� return fileContents;�� } catch(IOException ex) {� ex.printStackTrace();� return fileContents;� }� }�}
Multi-Catch
6
Without Multi-Catch
try {
// Code that may throw an exception
} catch(SomeExceptionType ex) {
// Code to handle the exception
} catch(OtherExceptionType ex) {
// Code to handle the exception
}
With Multi-Catch (added in version 7)
try {
// Code that may throw an exception
} catch(SomeExceptionType | OtherExeptionType ex) {
// Code to handle the exception
}
Checked Exceptions
7
The Handle or Declare Rule
8
Handle
try {
} catch(Exception ex) {
}
or
Declare
public void method throws Exception {
}
Finally Blocks
9
Try with Resources
public class TryWithResourcesExample {�� public String readFile(String fileName) throws IOException {� File file = new File(fileName);� String fileContents = "";�� try(FileReader fr = new FileReader(file);� BufferedReader br = new BufferedReader(fr)) {� � String line;� while((line = br.readLine()) != null) {� fileContents += line;� fileContents += "\n";� }�� return fileContents;� }� }�}
10
Throwing an Exception
public class ExceptionThrowingExample {�� public void myMethod(int methodParam) {� if(methodParam < 0) {� throw new IllegalArgumentException("The parameter may not be negative");� }� }�}
11
Rethrowing an Exception
public class ExceptionRethrowingExample {�� public void myMethod() {� try {� // Code that may throw different types of exceptions� } catch (RuntimeException ex) {� throw ex;� } catch (Exception ex) {� // Do something with all checked exceptions� }� }�}
12
Avoid swallowing RuntimeExceptions
The Overridden Method Exception Rule
13
Creating Your Own Exception Classes
public class ImageEditorException extends Exception {� public ImageEditorException() {� }�� public ImageEditorException(String message) {� super(message);� }�� public ImageEditorException(String message, Throwable cause) {� super(message, cause);� }�� public ImageEditorException(Throwable cause) {� super(cause);� }�}�
14
Citations
1. Diagrams created by course author, Jerod Wilkerson.