Sample of a Class public class Dog{ Creating an Object Dog obj = new Dog()
0 Comments
Java try block Java try block is used to enclose the code that might throw an exception. It must be used within the method. Java try block must be followed by either catch or finally block. Syntax of java try-catch
Syntax of try-finally block
Java catch block Java catch block is used to handle the Exception. It must be used after the try block only. You can use multiple catch block with a single try. Problem without exception handling Let's try to understand the problem if we don't use try-catch block.
If you run this code, you will get below exception Exception in thread main java.lang.ArithmeticException:/ by zero As displayed in the above example, rest of the code is not executed (in such case, rest of the code... statement is not printed). There can be 100 lines of code after exception. So all the code after exception will not be executed. Solution by exception handling Let's see the solution of above problem by java try-catch block
If you run the code now, you will get below output
Exception in thread main java.lang.ArithmeticException:/ by zero remaining code... |