Module lessons (1/2)
Try-catch blocks and exceptions
Checked vs unchecked exceptions, block order catch, multi-catch (A | B), custom exceptions and relaunch with cause.
What you'll learn in this lesson
- Distinguish Checked Exceptions (
IOException, handled or declared withthrows) from Unchecked / RuntimeException (IllegalArgumentException,NullPointerException). - Trap specific errors by ordering
catchblocks correctly and taking advantage of multi-catch (A | B). - Throw explicit exceptions (
throw new) while always preserving the original cause (cause).
Overview
When an operation fails at runtime (file missing, input malformed, division by zero), Java throws an object Exception up the Stack until a compatible block catch handles it.
Java checks the catch blocks from top to bottom choosing the first compatible one: always put the most specific exceptions before Exception. The optional block finally runs in any case (success or error):
Sections covered in this lesson
- 11. Hierarchy
Throwable: Checked vs Unchecked - 22. Order of
catch,finallyand Multi-Catch (|) - 33. Throwing Exceptions with
throw newand Exception Chaining - 44. Create a Lightweight Custom Exception
- 5Try it yourself
Unlock "Try-catch blocks and exceptions" and the full Course Java curriculum
Checked vs unchecked exceptions, block order `catch`, multi-catch (`A | B`), custom exceptions and relaunch with cause.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Course Java plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.