throw
throw
throw
Exception Handling is one of the effective means to handle runtime errors so that the
regular flow of the application can be preserved. Java Exception Handling is a
mechanism to handle runtime errors such as ClassNotFoundException, IOException,
SQLException, RemoteException, etc.
The throw keyword in Java is used to explicitly throw an exception from a method or
any block of code. We can throw either checked or unchecked exception. The throw
keyword is mainly used to throw custom exceptions.
throw Instance
Example:
throw new ArithmeticException("/ by zero");
The flow of execution of the program stops immediately after the throw statement is executed and the nearest enclosing try block is checked to see if it has a catch
statement that matches the type of exception. If it finds a match, controlled is transferred to that statement otherwise next enclosing try block is checked, and so on.
If no matching catch is found then the default exception handler will halt the program.