Concept Flow - Throw keyword
Start
Check condition
Yes|No
Throw Exception
Exception Propagates
Catch or Program Ends
The program checks a condition, and if true, it throws an exception which propagates until caught or program ends.
public class Test { public static void main(String[] args) { int age = 15; if (age < 18) { throw new IllegalArgumentException("Age must be 18 or older"); } } }
| Step | Action | Condition | Result | Exception Thrown |
|---|---|---|---|---|
| 1 | Set age = 15 | - | age = 15 | No |
| 2 | Check if age < 18 | 15 < 18 | True | No |
| 3 | Throw IllegalArgumentException | - | - | Yes: "Age must be 18 or older" |
| 4 | Program stops or exception propagates | - | - | Exception propagates |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| age | undefined | 15 | 15 | 15 |
Throw keyword in Java:
- Used to throw an exception explicitly
- Syntax: throw new ExceptionType("message");
- Stops normal flow and propagates exception
- Usually inside condition checks
- Must be caught or declared to avoid program crash