[1869 views]
The try block contains a sequence of statements that can throw an exception. The try block is always followed by a catch block that handles exceptions thrown by the corresponding try block. The try block must be followed by a catch block or finally block or both.
The catch block is where you handle exceptions, which must follow the try block. A single try block can have several link capture blocks. You can catch different types of exceptions in different catch blocks. When an exception occurs in the try block, the corresponding catch block that handles that particular exception is executed. For example, if an arithmetic exception occurs in the try block, the statements contained in the catch block for the arithmetic exception will be executed.
Write a java program to read 2 integers a and b. Compute a/b and print ,when b is not zero. Raise an exception when b is equal to zero.
Enter Numerator:21
Enter Denominator:7
Quotient=3
Enter Numerator:5
Enter Denominator:0
java.lang.ArithmeticException : / by zero