java exception handling try, throw, and catchintroduction creating an exception throwing a custom...

45
Introduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser 1 1 Kalamazoo College, Kalamazoo, Michigan USA COMP 210, Spring 2017

Upload: others

Post on 17-Jun-2020

28 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Java Exception Handlingtry, throw, and catch

Gerry Howser1

1Kalamazoo College, Kalamazoo, Michigan USA

COMP 210, Spring 2017

Page 2: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Outline

1 IntroductionExceptions in Java

2 Creating an ExceptionExtending the class Exception

3 throwing a Custom Exception

4 try-catch BlockstrycatchMore on Catching Exceptions

Page 3: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Exceptions in Java

Exceptions (a.k.a oops!)

BackgroundExceptions are all subclasses of the class throwable.Error subclass is for “hard” errors such as catastrophicerror.Run-time exceptions are for run-time errors that may (ormay not) be fixed on the fly.

Page 4: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Exceptions in Java

Exceptions (a.k.a oops!)

BackgroundExceptions are all subclasses of the class throwable.Error subclass is for “hard” errors such as catastrophicerror.Run-time exceptions are for run-time errors that may (ormay not) be fixed on the fly.

Page 5: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Exceptions in Java

Exceptions (a.k.a oops!)

BackgroundExceptions are all subclasses of the class throwable.Error subclass is for “hard” errors such as catastrophicerror.Run-time exceptions are for run-time errors that may (ormay not) be fixed on the fly.

Page 6: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Exceptions in Java

The Class throwable

Figure: The Class throwable

Page 7: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Exceptions in Java

Three Types of Exceptions

error subclassesTechnically, you can throw errors. Don’t.These are catastrophic failures and usually can’t be fixed bythe user

runtime error subclassesTechnically, you can throw these as well. Don’t.These are usually JAVA errors

exception errorsJava has many exceptions you might want to throw/catchThis is the best place to create your own exceptions

Page 8: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Exceptions in Java

Three Types of Exceptions

error subclassesTechnically, you can throw errors. Don’t.These are catastrophic failures and usually can’t be fixed bythe user

runtime error subclassesTechnically, you can throw these as well. Don’t.These are usually JAVA errors

exception errorsJava has many exceptions you might want to throw/catchThis is the best place to create your own exceptions

Page 9: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Exceptions in Java

Three Types of Exceptions

error subclassesTechnically, you can throw errors. Don’t.These are catastrophic failures and usually can’t be fixed bythe user

runtime error subclassesTechnically, you can throw these as well. Don’t.These are usually JAVA errors

exception errorsJava has many exceptions you might want to throw/catchThis is the best place to create your own exceptions

Page 10: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Exceptions in Java

Three Types of Exceptions

error subclassesTechnically, you can throw errors. Don’t.These are catastrophic failures and usually can’t be fixed bythe user

runtime error subclassesTechnically, you can throw these as well. Don’t.These are usually JAVA errors

exception errorsJava has many exceptions you might want to throw/catchThis is the best place to create your own exceptions

Page 11: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Exceptions in Java

Three Types of Exceptions

error subclassesTechnically, you can throw errors. Don’t.These are catastrophic failures and usually can’t be fixed bythe user

runtime error subclassesTechnically, you can throw these as well. Don’t.These are usually JAVA errors

exception errorsJava has many exceptions you might want to throw/catchThis is the best place to create your own exceptions

Page 12: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Exceptions in Java

Three Types of Exceptions

error subclassesTechnically, you can throw errors. Don’t.These are catastrophic failures and usually can’t be fixed bythe user

runtime error subclassesTechnically, you can throw these as well. Don’t.These are usually JAVA errors

exception errorsJava has many exceptions you might want to throw/catchThis is the best place to create your own exceptions

Page 13: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Exceptions in Java

Three Types of Exceptions

error subclassesTechnically, you can throw errors. Don’t.These are catastrophic failures and usually can’t be fixed bythe user

runtime error subclassesTechnically, you can throw these as well. Don’t.These are usually JAVA errors

exception errorsJava has many exceptions you might want to throw/catchThis is the best place to create your own exceptions

Page 14: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Exceptions in Java

What to throw

You should never throw an error, only an exception

What exceptions a method can throw must be documentedWhat exceptions a method can throw are part of themethod signatureYou must instantiate an exception when you throw itA good place to start looking:https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html

Page 15: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Exceptions in Java

What to throw

You should never throw an error, only an exception

What exceptions a method can throw must be documentedWhat exceptions a method can throw are part of themethod signatureYou must instantiate an exception when you throw itA good place to start looking:https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html

Page 16: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Exceptions in Java

What to throw

You should never throw an error, only an exception

What exceptions a method can throw must be documentedWhat exceptions a method can throw are part of themethod signatureYou must instantiate an exception when you throw itA good place to start looking:https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html

Page 17: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Exceptions in Java

What to throw

You should never throw an error, only an exception

What exceptions a method can throw must be documentedWhat exceptions a method can throw are part of themethod signatureYou must instantiate an exception when you throw itA good place to start looking:https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html

Page 18: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Exceptions in Java

What to throw

You should never throw an error, only an exception

What exceptions a method can throw must be documentedWhat exceptions a method can throw are part of themethod signatureYou must instantiate an exception when you throw itA good place to start looking:https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html

Page 19: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Outline

1 IntroductionExceptions in Java

2 Creating an ExceptionExtending the class Exception

3 throwing a Custom Exception

4 try-catch BlockstrycatchMore on Catching Exceptions

Page 20: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Extending the class Exception

Creating an Exception to throw

You should write your own exception classes if you answer yesto any of the following questions; otherwise, you can probablyuse someone else’s.

Do you need an exception type that isn’t represented bythose in the Java platform?Would it help users if they could differentiate yourexceptions from those thrown by classes written by othervendors?Does your code throw more than one related exception?If you use someone else’s exceptions, will users haveaccess to those exceptions?A similar question is, should your package be independentand self-contained?

Page 21: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Extending the class Exception

Creating an Exception to throw

You should write your own exception classes if you answer yesto any of the following questions; otherwise, you can probablyuse someone else’s.

Do you need an exception type that isn’t represented bythose in the Java platform?Would it help users if they could differentiate yourexceptions from those thrown by classes written by othervendors?Does your code throw more than one related exception?If you use someone else’s exceptions, will users haveaccess to those exceptions?A similar question is, should your package be independentand self-contained?

Page 22: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Extending the class Exception

Creating an Exception to throw

You should write your own exception classes if you answer yesto any of the following questions; otherwise, you can probablyuse someone else’s.

Do you need an exception type that isn’t represented bythose in the Java platform?Would it help users if they could differentiate yourexceptions from those thrown by classes written by othervendors?Does your code throw more than one related exception?If you use someone else’s exceptions, will users haveaccess to those exceptions?A similar question is, should your package be independentand self-contained?

Page 23: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Extending the class Exception

Creating an Exception to throw

You should write your own exception classes if you answer yesto any of the following questions; otherwise, you can probablyuse someone else’s.

Do you need an exception type that isn’t represented bythose in the Java platform?Would it help users if they could differentiate yourexceptions from those thrown by classes written by othervendors?Does your code throw more than one related exception?If you use someone else’s exceptions, will users haveaccess to those exceptions?A similar question is, should your package be independentand self-contained?

Page 24: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Extending the class Exception

Creating an Exception to throw

You should write your own exception classes if you answer yesto any of the following questions; otherwise, you can probablyuse someone else’s.

Do you need an exception type that isn’t represented bythose in the Java platform?Would it help users if they could differentiate yourexceptions from those thrown by classes written by othervendors?Does your code throw more than one related exception?If you use someone else’s exceptions, will users haveaccess to those exceptions?A similar question is, should your package be independentand self-contained?

Page 25: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Extending the class Exception

Guidelines for Your Exceptions

For readable code, it’s good practice to append the stringException to the names of all classes that inherit (directlyor indirectly) from the Exception class.If a client can reasonably be expected to recover from anexception, make it a checked exception. If a client cannotdo anything to recover from the exception, make it anunchecked exception.

Page 26: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Extending the class Exception

Guidelines for Your Exceptions

For readable code, it’s good practice to append the stringException to the names of all classes that inherit (directlyor indirectly) from the Exception class.If a client can reasonably be expected to recover from anexception, make it a checked exception. If a client cannotdo anything to recover from the exception, make it anunchecked exception.

Page 27: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Extending the class Exception

Creating a StackEmptyException

Suppose you wish to create an exception if a user tries topop() an empty stack.

Change the method signature: element method pop()throws StackEmptyException

Create a class file named StackEmptyException

Usually, you can simply create a new class and then use it:public class StackEmptyException extendsException

Page 28: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Extending the class Exception

Creating a StackEmptyException

Suppose you wish to create an exception if a user tries topop() an empty stack.

Change the method signature: element method pop()throws StackEmptyException

Create a class file named StackEmptyException

Usually, you can simply create a new class and then use it:public class StackEmptyException extendsException

Page 29: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Extending the class Exception

Creating a StackEmptyException

Suppose you wish to create an exception if a user tries topop() an empty stack.

Change the method signature: element method pop()throws StackEmptyException

Create a class file named StackEmptyException

Usually, you can simply create a new class and then use it:public class StackEmptyException extendsException

Page 30: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Extending the class Exception

Creating a StackEmptyException

Suppose you wish to create an exception if a user tries topop() an empty stack.

Change the method signature: element method pop()throws StackEmptyException

Create a class file named StackEmptyException

Usually, you can simply create a new class and then use it:public class StackEmptyException extendsException

Page 31: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Outline

1 IntroductionExceptions in Java

2 Creating an ExceptionExtending the class Exception

3 throwing a Custom Exception

4 try-catch BlockstrycatchMore on Catching Exceptions

Page 32: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

pop() with Error

public element pop() throwsStackEmptyException {if (this.isEmpty()){throw new StackEmptyException;}...}

Page 33: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

Outline

1 IntroductionExceptions in Java

2 Creating an ExceptionExtending the class Exception

3 throwing a Custom Exception

4 try-catch BlockstrycatchMore on Catching Exceptions

Page 34: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

try

Using Exceptions

There are two types of exceptions (for now)Checked Exceptions (exceptions the user can fix)If the user can fix it, catch itUnchecked Exceptions (exceptions the user can’t fix)No one can help, don’t catch it.Exceptions that are not caught abort execution.

Page 35: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

try

Using Exceptions

There are two types of exceptions (for now)Checked Exceptions (exceptions the user can fix)If the user can fix it, catch itUnchecked Exceptions (exceptions the user can’t fix)No one can help, don’t catch it.Exceptions that are not caught abort execution.

Page 36: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

try

Using Exceptions

There are two types of exceptions (for now)Checked Exceptions (exceptions the user can fix)If the user can fix it, catch itUnchecked Exceptions (exceptions the user can’t fix)No one can help, don’t catch it.Exceptions that are not caught abort execution.

Page 37: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

try

Using Exceptions

There are two types of exceptions (for now)Checked Exceptions (exceptions the user can fix)If the user can fix it, catch itUnchecked Exceptions (exceptions the user can’t fix)No one can help, don’t catch it.Exceptions that are not caught abort execution.

Page 38: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

try

Using Exceptions

There are two types of exceptions (for now)Checked Exceptions (exceptions the user can fix)If the user can fix it, catch itUnchecked Exceptions (exceptions the user can’t fix)No one can help, don’t catch it.Exceptions that are not caught abort execution.

Page 39: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

try

Using Exceptions

There are two types of exceptions (for now)Checked Exceptions (exceptions the user can fix)If the user can fix it, catch itUnchecked Exceptions (exceptions the user can’t fix)No one can help, don’t catch it.Exceptions that are not caught abort execution.

Page 40: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

try

try Blocks

If you can fix the error (with or without user “help”) you shouldsurround statements that can cause the error with a try block.try {target = myStack.pop();}

Page 41: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

catch

catch Blocks

Once an exception has been thrown, you have three choicesIgnore the error and allow the program to terminatecatch the error and fix itcatch the error and have the user fix it (enter a differentfile name????)

Page 42: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

catch

try-catch Blocks

Figure: From:http://beginnersbook.com/2013/05/flow-in-try-catch-finally/

Page 43: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

More on Catching Exceptions

Multiple catch Blocks

If a try block can throw more than one exception that you canfix, you will need a catch block for each one.If you wish some common code to be executed after exceptionsoccur, you can place that in a finally block.

Page 44: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

More on Catching Exceptions

Example of try-catch-finally Blocks

Figure: http://beginnersbook.com/2013/05/flow-in-try-catch-finally/

Page 45: Java Exception Handling try, throw, and catchIntroduction Creating an Exception throwing a Custom Exception try-catch Blocks Java Exception Handling try, throw, and catch Gerry Howser1

Introduction Creating an Exception throwing a Custom Exception try-catch Blocks

More on Catching Exceptions

Output from Example

Figure: Output from the Example