Chapter 11: Input/Output and Exception Handling
Catching Exceptions
- A good rule of thumb is throw early, catch late
- Below is some code that shows the Java syntax
- There can be any number of catch clauses (including 0).
- There can be 0 or 1 finally clauses.
try
{
// code that might generate an IOException or a FileNotFoundException
}
catch (IOException exception)
{
// handle the exception
}
catch (FileNotFoundException exception)
{
// handle the exception
}
finally
{
// this code gets executed regardless of what else occurred
// e.g. out.close();
}
Custom Exceptions
public class WhateverException extends RunTimeException
{
public WhateverException()
{
}
public WhateverException(String message)
{
super(message);
}
}
Lecture Code