If the age is below 18, raise an exception displaying “Access denied”. Otherwise, if the age is 18 or older, print “Access granted”.
public
|
The throws keyword specifies the type of exception that a method may throw, with various exception types accessible in Java such as ArithmeticException, ClassNotFoundException, ArrayIndexOutOfBoundsException, SecurityException, and more.
Distinguishing between throw and throws:
throw |
throws |
employed to raise an exception within a method |
It’s used to specify the type of exception that a method may throw. |
It’s not possible to throw multiple exceptions. |
It’s possible to declare multiple exceptions. |
Syntax
|
Syntax
|