Java Throws Keyword - W3Schools
Maybe your like
❮ Java Keywords
Example
Throw an exception if age is below 18 (print "Access denied"). If age is 18 or older, print "Access granted":
public class Main { static void checkAge(int age) throws ArithmeticException { if (age < 18) { throw new ArithmeticException("Access denied - You must be at least 18 years old."); } else { System.out.println("Access granted - You are old enough!"); } } public static void main(String[] args) { checkAge(15); // Set age to 15 (which is below 18...) } }Try it Yourself »
Definition and Usage
The throws keyword indicates what exception type may be thrown by a method.
There are many exception types available in Java: ArithmeticException, ClassNotFoundException, ArrayIndexOutOfBoundsException, SecurityException, etc.
Differences between throw and throws:
| throw | throws |
|---|---|
| Used to throw an exception for a method | Used to indicate what exception type may be thrown by a method |
| Cannot throw multiple exceptions | Can declare multiple exceptions |
Syntax:
| Syntax:
|
Related Pages
Read more about exceptions in our Java Try..Catch Tutorial.
❮ Java Keywords
★ +1 Sign in to track progressTag » How To Throw Exception In Java
-
How To Throw Exceptions In Java - Rollbar
-
How To Use The Throws Keyword In Java (and When To Use ... - Rollbar
-
How To Throw Exceptions (The Java™ Tutorials > Essential Java ...
-
Java Throw Exception - Javatpoint
-
Throw And Throws In Java - GeeksforGeeks
-
Try, Catch, Throw And Throws In Java - GeeksforGeeks
-
How To Throw An Exception In Java | Webucator
-
How To Throw Exception In Java With Example
-
How To Throw An Exception In Java - Linux Hint
-
How Can I Raise An Error In If-else Function In Java - Stack Overflow
-
Java Throw And Throws Keyword - Programiz
-
Throwing An Exception In Java - Javamex
-
How To Throw Exceptions In Java - The Differences Between Throw ...
-
Types Of Exceptions In Java - Stackify