What happens when something fails, and who may see what.
Key points, notes and past questions
Exception Handling and Assertions
Where control goes when something fails.
- A throw leaves the line at once
- A throwing statement never finishes; the rest of its block is skipped.
- Unfinished calls are removed newest first until a handler fits.
- The first matching
catchruns, and only that one. - After a handler, execution continues below the whole
try-catch. - With no matching handler, earlier output stays and the program ends with an error.
- Your own types: checked or unchecked
- A custom exception passes its text to
super;getMessage()returns it. - Extending
Exceptionmakes it checked; extendingRuntimeExceptionorErrormakes it unchecked. - Checked: catch it or declare it with
throws, or the code does not compile. - A throwing constructor assigns nothing.
println(e)shows the class name;getMessage()does not.
- A custom exception passes its text to
- Catch it, note it, throw it on
throw e;passes on the same object.- The nearer handler prints first.
- Later
catchblocks of the sametrydo not apply. - A rethrown checked exception must be declared.
- Without the rethrow, callers never see the exception.
- Keeping the first reason
initCausestores one exception inside another.- The wrapper's type is what
catchmatches. getMessage()is the outer text;getCause().getMessage()is the inner text.- No cause means
getCause()isnull. - A cause is set at most once.
- Assertions run in order
assert cond : detail;throwsAssertionErrorwhen enabled and false.- The first false check reached ends the normal flow.
- A
nullcall inside a condition throwsNullPointerExceptioninstead. - An empty string passes a
nulltest. AssertionErroris not caught bycatch (Exception e).
- Switching assertions on
- Assertions are off unless the command enables them.
-ea: all your classes;-ea:Name: that class only.-da:Nameremoves one class from-ea.- A disabled check does not calculate its condition.
- The compile step is the same either way.
Notes19 past questions in this topic
Packages and Access Modifiers
Who may see a member, within and across packages.
- Packages, imports and full names
- A full name is package plus class name.
- An import only shortens a name inside one file.
- Order: single-class import, own package,
*import. - Two single-class imports of one simple name are rejected; two stars fail only when the name is used.
- The four access levels
- Order:
private, no modifier,protected,public. privateis per class: one object may read another object's private field there.- No modifier and
protecteddiffer only for subclasses outside the package. - Overload choice ignores methods the caller may not use.
- A top-level class is
publicor package-private.
- Order:
- Protected across packages
- Inside the declaring package,
protectedbehaves like package access. - Outside, the line must be inside a subclass.
- The qualifier must be that subclass or lower;
super.m()is also fine. - A non-subclass is refused even through a subclass variable.
- Inside the declaring package,
- The static type and access
- The static type decides whether a call compiles.
- The runtime class decides which override runs.
- An override may widen access, never narrow it.
- A private or hidden package-private parent method is not overridden.
Notes9 past questions in this topic
Week 6: Collections and mapsWeek 8: Cloning, type inference, higher-order functions, streamsAll Java topics and weeks