Without a try/catch, an error like division-by-zero or bad input (InputMismatchException) crashes the whole program. Handling it lets the program recover or fail gracefully.
Everything throwable extends Throwable → splits into Exception (recoverable problems) and Error (serious JVM-level problems, not meant to be caught).
Exception (excluding RuntimeException)throws) themIOException, SQLExceptionRuntimeExceptionNullPointerException, ArithmeticExceptionWhen an exception isn't caught in the current method, Java "unwinds" back through the call stack, exiting each calling method in turn, until a matching catch is found (or the program terminates).
Gives your program's own, meaningful exception types (e.g. InsufficientFundsException) instead of forcing every error into a generic one — makes calling code's catch clauses more precise.
A condition that must be true before a method runs (e.g. "amount must be positive").
A condition guaranteed true after a method completes successfully.
Any resource implementing AutoCloseable (files, streams, DB connections) is automatically closed when the block ends — even if an exception occurs — eliminating manual finally { close(); } boilerplate.
| Method | Purpose |
|---|---|
getMessage() | returns the exception's detail message |
getCause() | returns the underlying exception that caused this one (chaining) |
printStackTrace() | prints the full call-stack trace to System.err |
getStackTrace() | returns the stack trace as a StackTraceElement[] |
toString() | class name + message as text |
addSuppressed(e) | records extra exceptions suppressed during try-with-resources cleanup |