throw Keyword
Throwing exceptions manually
4 min read
throw Keyword
The throw keyword is used to explicitly throw an exception.
Code Examples
Manually throwing an exception
java
1
2if (age < 18) {
3 throw new IllegalArgumentException("Age must be 18+");
4}
5 Use Cases
- Business rule validation
- Custom error signaling
Common Mistakes to Avoid
- Throwing generic exceptions
- Not documenting thrown exceptions