Exception means abnormal condition also known as runtime errors, it terminates program abnormal in between the execution of the program, So to handle runtime errors we use Exceptional handling.
To handle exceptions we use:
Example 1: Java Exceptional handling, ArithmeticException, Divide by 0.
package divisionmethod; // find division of two numbers using method public class Divisionmethod { public static void main(String[] args) { try{ int a=10, b=0, c; c=a/b; System.out.println("Division = " + c); } catch(ArithmeticException ex) { System.out.println("Cannot divide by 0"); } } }
Example 2: Java Exceptional handling, ArithmeticException, Divide by 0.
package divisionmethod; // find division of two numbers using method public class Divisionmethod { public void division(int a,int b) throws ArithmeticException { int d = a/b; System.out.println("Division = " + d); } public static void main(String[] args) { Divisionmethod m = new Divisionmethod(); try{ m.division(4, 0); } catch(ArithmeticException ex) { System.out.println("Cannot divide by 0, ArithmeticException found"); } } }