If the caller provided a path
& name
that fails..
The catch
block logs the issue for debugging and tries to recover using a default.
(If that fails, the exception "escapes".)
This should be a double
, but if it's not..
The catch
block stops an implementation-specific exception from "escaping" and replaces it with a detailed, clear exception suitable for public consumption.
If something goes wrong accessing the DB..
The catch
block cleans up before re-throwing the error, like a targeted finally
.
(Should they have used finally
instead? Maybe!)
// REQUIRES: f is a writable file; there
// is enough disk space to store image
// MODIFIES: f
// EFFECTS: writes image to f
public void save(File f, Image image) { ... }
We might instead check for conditions we require or surprising circumstances.
Exceptions give us a way to
signal exceptional circumstances,
handle them if possible, and
crash the program otherwise
}
Puts a lot of burden on the caller,
some of which is hard to check!
// MODIFIES: f
// EFFECTS: writes image to f if possible
// if f is not writable, throws IOException
// if there is not enough disk space,
// throws DiskFullException
public void save(File f, Image image) throws
IOException, DiskFullException { ... }
}
// MODIFIES: f
// EFFECTS: writes image to f if possible
// if f is not a writable file, ?????
// if there is not enough disk space, ????
public void save(File f, Image image) { ... }
}
throw
Statementthrow new ExceptionClass(...);
Any expression with an apparent type that is a subtype of Exception
(including Exception
)
(Technically Throwable
, not Exception
.)
To execute the throw
:
previousStatement(); try { ... } catch (SomeException e) { ... } finally { ... } nextStatement();
Normal flow of control for a try
:
previousStatement(); try { ... } catch (SomeException e) { ... } finally { ... } nextStatement();
an exception is thrown from inside the try
block and exits (ends) that block.
If a catch
matches (the first that can be assigned the exception), normal execution resumes there.
Normal flow of control from catch
:
A catch
gets a chance if..
previousStatement(); try { ... } catch (SomeException e) { ... } finally { ... } nextStatement();
an exception is thrown from inside the try
block and exits (ends) that block.
If no catch
matches, the exception exits (ends) the try/catches.
A catch
gets a chance if..
Or: If a catch
did match but then it threw an exception, that also exits (ends) the try/catches.
then resumes the exception. Either way, the try statement ends with the exception.
If there is a finally, it executes normally..
assert someBooleanExpression; // or
assert someBooleanExpression : valueExpression;
An expression with an apparent type of boolean
to check.
To execute the assert
:
boolean
expression normally.true
, continue to the next statement.
But: If assertions are disabled, the whole statement is ignored.
Used as the error message on failure
Throwable
Object
Error
Exception
RuntimeException
...
...
Unchecked Exceptions
Checked Exceptions