Java throws keyword

Java throws keyword


Throws Keyword In Java
A throws clause lists the types of exceptions that a method might throw. 
This is necessary for all exceptions, except those of type Error or RuntimeException, or any of their subclasses. 
All other exceptions that a method can throw must be declared in the throws clause.
If they are not, a compile-time error will result.
Now we will see an example - 
Example - 
// Web designing house
// Online Promotion House

class ThrowsDemo {
static void throwOne() throws IllegalAccessException {
System.out.println("Inside throwOne.");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
try {
throwOne();
} catch (IllegalAccessException e) {
System.out.println("Caught " + e);
}
}
}
Output - 
The sample output would be -  
inside throwOne
Caughtjava.lang.IllegalAccessException: demo