0% found this document useful (0 votes)
42 views

05 Exceptions

The document discusses exceptions in Java programs. It explains that exceptions can be caused by errors from the end user, programmer, or environment. It provides an example of a program that prompts the user for two integers and divides them, causing an ArithmeticException when the user enters 0 for the second integer. The document then discusses checked and unchecked exceptions, how exceptions are handled using try/catch blocks, and examples of using try/catch to handle FileNotFoundExceptions.

Uploaded by

Jack
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

05 Exceptions

The document discusses exceptions in Java programs. It explains that exceptions can be caused by errors from the end user, programmer, or environment. It provides an example of a program that prompts the user for two integers and divides them, causing an ArithmeticException when the user enters 0 for the second integer. The document then discusses checked and unchecked exceptions, how exceptions are handled using try/catch blocks, and examples of using try/catch to handle FileNotFoundExceptions.

Uploaded by

Jack
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Steven Castellucci

 There are three sources that can lead to


exceptions:

◦ The End User


 Garbage-in, garbage-out

◦ The Programmer
 Misunderstanding requirements and/or contract

◦ The Environment
 The VM, OS, hardware, etc.

EECS1021 W16 (Steven C.) 2


Given two integers, write a program to
compute and output their quotient.

System.out.println("Enter the first integer:");


int a = input.nextInt();
System.out.println("Enter the second:");
int b = input.nextInt();
int c = a / b;
System.out.println("Their quotient is: " + c);

EECS1021 W16 (Steven C.) 3


Enter the first integer:
8
Enter the second:
0
Exception in thread "main"
java.lang.ArithmeticException: / by zero
at Quotient.main(Quotient.java:16)

In this case:
- The error source is the end user.
- The incorrect operation is invalid
- The exception was not caught

EECS1021 W16 (Steven C.) 4


Enter the first integer:
8
Enter the second:
0
Exception in thread "main"
java.lang.ArithmeticException: / by zero
at Quotient.main(Quotient.java:16)

Type Stack trace Message

EECS1021 W16 (Steven C.) 5


 Unchecked
◦ Does not need to be handled by code
◦ Derives from RuntimeException class:
 ArithmeticException
 ArrayIndexOutOfBoundsException
 …
 Checked
◦ Needs to be handled by code (e.g., “throws” clause)
◦ Derives from Exception class:
 FileNotFoundException
 IOException
 …

EECS1021 W16 (Steven C.) 6


•An error source can lead to an incorrect operation
•An incorrect operations may be valid or invalid
•An invalid operation throws an exception
•An exception becomes a runtime error unless caught

Sources
Programmer,
End User, or Logic
Handler
Environment Error

yes yes
Error
Incorrect Valid
Exception Caught? Runti me
Operations Operation? no no Error

EECS1021 W16 (Steven C.) 7


try
{ ...
code fragment
...
}
catch (SomeType e)
{ ...
exception handler
...
}
program continues

EECS1021 W16 (Steven C.) 8


 Example 1:
◦ Prompt for a filename and output a message if the
file is not found

 Example 2:
◦ Prompt for a filename
◦ Loop until the user provides a valid filename

EECS1021 W16 (Steven C.) 9


Scanner input = new Scanner(System.in);
Scanner fileInput;
try
{
System.out.print("Enter filename: ");
fileInput = new Scanner(new File(input.nextLine()));
System.out.println("File opened.");
}
catch (FileNotFoundException fnfe)
{
System.out.println("File not found!");
}

EECS1021 W16 (Steven C.) 10


for (boolean repeat = true; repeat; )
{
try
{
System.out.print("Enter filename: ");
fileInput = new Scanner(new File(input.nextLine()));
repeat = false;
}
catch (FileNotFoundException fnfe)
{
System.out.println("File not found! Try again.");
}
}
System.out.println("File opened.");

EECS1021 W16 (Steven C.) 11


try
{ ...
}
catch (Type-1 e)
{ ...
}
catch (Type-2 e)
{ ...
}
...
catch (Type-n e)
{ ...
}
program continues
EECS1021 W16 (Steven C.) 12
 Each different exception class represents a
different (invalid) situation

 Catching a specific exception allows you to


handle that particular circumstance

 Example:
◦ When you catch the FileNotFoundException, you
know that the file you attempted to access does not
exist

EECS1021 W16 (Steven C.) 13


 Demonstrated in lecture

EECS1021 W16 (Steven C.) 14

You might also like