![Page 1: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/1.jpg)
James TamException handling in Java
Java Exception Handling
Dealing with errors using Java’s exception handling mechanism
![Page 2: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/2.jpg)
James TamException handling in Java
Approaches For Dealing With Error Conditions
Use conditional statements and return values
Use Java’s exception handling mechanism
![Page 3: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/3.jpg)
James TamException handling in Java
Approaches For Dealing With Error Conditions
Use conditional statements and return values
Use Java’s exception handling mechanism
![Page 4: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/4.jpg)
James TamException handling in Java
Class Inventory: An Earlier Example
class Inventory{
public boolean addToInventory (int amount) { int temp = stockLevel + amount; if (temp > MAX) { System.out.print("Adding " + amount + " item will cause stock "); System.out.println("to become greater than " + MAX + " units");
return false; } else { stockLevel = stockLevel + amount;
return true; } } :
![Page 5: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/5.jpg)
James TamException handling in Java
Some Hypothetical Method Calls: Condition/Return
store.addToInventory (int amt)
If (temp > MAX)
return false;
object2.method2 ()
If (store.addToInventory(amt) == false)
return false;
object1.method1 ()
If (object2.method2() == false)
return false;
![Page 6: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/6.jpg)
James TamException handling in Java
Some Hypothetical Method Calls: Condition/Return
store.addToInventory (int amt)
If (temp > MAX)
return false;
object2.method2 ()
If (store.addToInventory(amt) == false)
return false;
object1.method1 ()
If (object2.method2() == false)
return false;
Problem 1: The calling method may forget to check the return value
![Page 7: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/7.jpg)
James TamException handling in Java
Some Hypothetical Method Calls: Condition/Return
store.addToInventory (int amt)
If (temp > MAX)
return false;
object2.method2 ()
If (store.addToInventory(amt) == false)
return false;
object1.method1 ()
If (object2.method2() == false)
return false;
Problem 2: A long series of method calls requires many checks/returns
![Page 8: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/8.jpg)
James TamException handling in Java
Some Hypothetical Method Calls: Condition/Return
store.addToInventory (int amt)
If (temp > MAX)
return false;
object2.method2 ()
If (store.addToInventory(amt) == false)
return false;
object1.method1 ()
If (object2.method2() == false)
return false;
Problem 3: The calling method may not know how to handle the error
??? ???
![Page 9: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/9.jpg)
James TamException handling in Java
Approaches For Dealing With Error Conditions
Use conditional statements and return values
Use Java’s exception handling mechanism
![Page 10: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/10.jpg)
James TamException handling in Java
Handling Exceptions
Format:try
{
// Code that may cause an exception to occur
}
catch (ExceptionType identifier)
{
// Code to handle the exception
}
![Page 11: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/11.jpg)
James TamException handling in Java
Handling Exceptions: An Example Revisited
The complete program can be found in the directory:/home/profs/tamj/233/examples/exceptions/handlingExceptions/firstExample
class SimpleIO{ public static void main (String [] argv) {
: try { fw = new FileWriter (filename);
: }
catch (IOException e) {
:}
![Page 12: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/12.jpg)
James TamException handling in Java
Handling Exceptions: An Example Revisited
FileWriter (String filename)
{
:
}SimpleIO.main ()
try
{
fw = new FileWriter (filename);
}
catch (IOException e)
{
:
}
![Page 13: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/13.jpg)
James TamException handling in Java
Handling Exceptions: An Example Revisited
FileWriter (String filename)
{
}SimpleIO.main ()
try
{
fw = new FileWriter (filename);
}
catch (IOException e)
{
:
}
Oops!
Can’t write to file
![Page 14: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/14.jpg)
James TamException handling in Java
Handling Exceptions: An Example Revisited
FileWriter (String filename)
{
}SimpleIO.main ()
try
{
fw = new FileWriter (filename);
}
catch (IOException e)
{
:
}
IOException thrown
IOException e= new IOException ()
![Page 15: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/15.jpg)
James TamException handling in Java
Handling Exceptions: An Example Revisited
FileWriter (String filename)
{
}SimpleIO.main ()
try
{
fw = new FileWriter (filename);
}
catch (IOException e)
{
:
}
IOException thrown
IOException e= new IOException ()
![Page 16: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/16.jpg)
James TamException handling in Java
Handling Exceptions: An Example Revisited
FileWriter (String filename)
{
}SimpleIO.main ()
try
{
fw = new FileWriter (filename);
}
catch (IOException e)
{
}
IOException must be
dealt with here
![Page 17: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/17.jpg)
James TamException handling in Java
try { fw = new FileWriter (filename); pw = new PrintWriter (fw);
pw.println(iw1.getNum()); pw.close(); fr = new FileReader(filename); br = new BufferedReader(fr);
System.out.println("Read from file: " + br.readLine());
}
Handling Exceptions: An Example Revisited
![Page 18: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/18.jpg)
James TamException handling in Java
try { fw = new FileWriter (filename); pw = new PrintWriter (fw);
pw.println(iw1.getNum()); pw.close(); fr = new FileReader(filename); br = new BufferedReader(fr);
System.out.println("Read from file: " + br.readLine());
}
Handling Exceptions: An Example Revisited
Exception 1
![Page 19: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/19.jpg)
James TamException handling in Java
Where The Exceptions Occur In Class FileWriter
For online documentation for this class go to:http://java.sun.com/j2se/1.4.1/docs/api/java/io/FileWriter.html
Class FileWriter
{
public FileWriter (String fileName) throws IOException;
public FileWriter (String fileName, boolean append) throws IOException;
:
}
![Page 20: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/20.jpg)
James TamException handling in Java
try { fw = new FileWriter (filename); pw = new PrintWriter (fw);
pw.println(iw1.getNum()); pw.close(); fr = new FileReader(filename); br = new BufferedReader(fr);
System.out.println("Read from file: " + br.readLine());
}
Handling Exceptions: An Example Revisited
Exception 2
![Page 21: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/21.jpg)
James TamException handling in Java
Where The Exceptions Occur In Class FileReader
For online documentation for this class go to:http://java.sun.com/j2se/1.4.1/docs/api/java/io/FileReader.html
Class FileReader
{
public FileReader (String fileName) throws FileNotFoundException;
public FileReader (File file) throws FileNotFoundException;
:
}
![Page 22: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/22.jpg)
James TamException handling in Java
try { fw = new FileWriter (filename); pw = new PrintWriter (fw);
pw.println(iw1.getNum()); pw.close(); fr = new FileReader(filename); br = new BufferedReader(fr);
System.out.println("Read from file: " + br.readLine());
}
Handling Exceptions: An Example Revisited
Exception 3
![Page 23: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/23.jpg)
James TamException handling in Java
Where The Exceptions Occur In Class BufferedReader
For online documentation for this class go to:http://java.sun.com/j2se/1.4.1/docs/api/java/io/BufferedReader.html
Class BufferedReader
{
public BufferedReader (Reader in);
public BufferedReader (Reader in, int sz);
public String readLine () throws IOException;
:
}
![Page 24: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/24.jpg)
James TamException handling in Java
Handling Exceptions: An Example Revisited
catch (IOException e)
{
System.out.println("File IO error: Exception thrown");
System.out.println(e);
System.out.println();
e.printStackTrace();
}
![Page 25: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/25.jpg)
James TamException handling in Java
Handling Exceptions: An Example Revisited
catch (IOException e)
{
System.out.println("File IO error: Exception thrown");
System.out.println(e);
System.out.println();
e.printStackTrace();
}
java.io.FileNotFoundException: data (No such file or directory)
java.io.FileNotFoundException: data (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:103)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at java.io.FileReader.<init>(FileReader.java:41)
at SimpleIO.main(SimpleIO.java:35)
![Page 26: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/26.jpg)
James TamException handling in Java
Common Exceptions
NullPointerException
ArrayIndexOutOfBoundsException
ArithmeticException
![Page 27: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/27.jpg)
James TamException handling in Java
Common Exceptions: An Example
int [] arr = null;
arr[0] = 1;
arr = new int [4];
int i;
for (i = 0; i <= 4; i++)
arr[i] = i;
arr[i-1] = arr[i-1] / 0;
NullPointerException
![Page 28: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/28.jpg)
James TamException handling in Java
Common Exceptions: An Example
int [] arr = null;
arr[0] = 1;
arr = new int [4];
int i;
for (i = 0; i <= 4; i++)
arr[i] = i;
arr[i-1] = arr[i-1] / 0;
ArrayIndexOutOfBoundsException
(when i = 4)
![Page 29: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/29.jpg)
James TamException handling in Java
Common Exceptions: An Example
int [] arr = null;
arr[0] = 1;
arr = new int [4];
int i;
for (i = 0; i <= 4; i++)
arr[i] = i;
arr[i-1] = arr[i-1] / 0;
ArithmeticException
(Division by zero)
![Page 30: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/30.jpg)
James TamException handling in Java
Categories Of Exceptions
Unchecked exceptions
Checked exception
![Page 31: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/31.jpg)
James TamException handling in Java
Unchecked Exceptions
• The compiler doesn’t require you to handle them if they are thrown.
• They can occur at any time in the program (not just for a specific method)
• Typically they are fatal runtime errors that are beyond your control
•Use conditional statements rather than the exception handling model.
• Examples: NullPointerException,IndexOutOfBoundsException, ArithmeticException…
![Page 32: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/32.jpg)
James TamException handling in Java
Checked Exceptions
Must be handled if they are ever thrown•Use a try-catch block
Deal with problems that occur in a specific place•When a particular method invoked
Example: IOException
![Page 33: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/33.jpg)
James TamException handling in Java
Avoid Squelching Your Exceptions
try
{
fw = new FileWriter (filename);
}
catch (IOException e)
{
// Do nothing here. Just set up the try-catch block to bypass those pesky
// syntax errors.
}
![Page 34: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/34.jpg)
James TamException handling in Java
Avoid Squelching Your Exceptions
try
{
fw = new FileWriter (filename);
}
catch (IOException e)
{
// Do nothing here. Just set up the try-catch block to bypass those pesky
// syntax errors.
}
NO!
![Page 35: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/35.jpg)
James TamException handling in Java
The Finally Clause
Part of Java’s exception handling model (try-catch-finally)
Used to enclose statements that must always be executed.
![Page 36: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/36.jpg)
James TamException handling in Java
The Finally Clause
try
{
}
catch
{
}
finally
{
}
![Page 37: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/37.jpg)
James TamException handling in Java
The Finally Clause: Exception Thrown
try
{
}
catch
{
}
finally
{
}
2) Exception is thrown and caught
3) Control transfers to the finally clause
1) Statements in the try block are attempted
![Page 38: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/38.jpg)
James TamException handling in Java
The Finally Clause: No Exception Occurs
try
{
}
catch
{
}
finally
{
}
1) Statements in the try block are completed
2) Control transfers to the finally clause
![Page 39: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/39.jpg)
James TamException handling in Java
Try-Catch-Finally: An Example
The complete program can be found in the directory:/home/profs/tamj/233/examples/exceptions/handlingExceptions/secondExample
![Page 40: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/40.jpg)
James TamException handling in Java
Try-Catch-Finally: An Example (2)
try{ BufferedReader br = new BufferedReader(new FileReader("phil")); String s = br.readLine(); while (s != null) s = br.readLine(); return;}catch (IOException e){ e.printStackTrace(); return;}finally{ System.out.println("<<<Finished reading>>>"); return;}
![Page 41: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/41.jpg)
James TamException handling in Java
When The Caller Can’t Handle The Exceptions
main ()
method 1 ()
method 2 ()
Exception thrown!
???
![Page 42: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/42.jpg)
James TamException handling in Java
When The Caller Can’t Handle The Exceptions: An Example
The complete program can be found in the directory:/home/profs/tamj/233/examples/exceptions/handlingExceptions/thirdExample
import java.io.*;
class IntermediateIO
{
public static void main (String [] argv)
{
method1 ();
}
![Page 43: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/43.jpg)
James TamException handling in Java
When The Caller Can’t Handle The Exceptions: An Example (2)
public static void method1 () { try { method2 (); return; } catch (IOException e) { System.out.println("IOException thrown while reading input file"); e.printStackTrace(); return; } }
![Page 44: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/44.jpg)
James TamException handling in Java
When The Caller Can’t Handle The Exceptions: An Example (3)
public static void method2 () throws IOException { BufferedReader br = null; String s;
br = new BufferedReader(new FileReader("phil")); s = br.readLine(); while (s != null) { System.out.println(s); s = br.readLine(); } return; }
![Page 45: James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism](https://reader036.vdocuments.mx/reader036/viewer/2022062313/56649d255503460f949fbe57/html5/thumbnails/45.jpg)
James TamException handling in Java
Summary
Handling exceptions with the try-catch block
Checked vs. unchecked exceptions
Using the finally clause to guarantee the execution of clean-up statements regardless of whether an exception occurs or not.