Home »
Java »
Java find output programs
Java find output programs (Exception Handling) | set 1
Find the output of Java programs | Exception Handling | Set 1: Enhance the knowledge of Java Exception Handling concepts by solving and finding the output of some Java programs.
Submitted by Nidhi, on February 04, 2021
Question 1:
public class ExpEx {
public static void main(String[] args) {
try {
int num1 = 10;
int num2 = 0;
int num3 = 0;
num3 = num1 / num2;
System.out.println("Result: " + num3);
}
}
}
Output:
/ExpEx.java:3: error: 'try' without 'catch', 'finally' or resource declarations
try {
^
1 error
Explanation:
The above program will generate syntax error because we cannot use the try block without catch or finally block in Java program.
Question 2:
public class ExpEx {
public static void main(String[] args) {
try {
int num1 = 10;
int num2 = 0;
int num3 = 0;
num3 = num1 / num2;
System.out.println("Result: " + num3);
}
finally {
System.out.println("Finally block executed...");
}
}
}
Output:
Finally block executed...
Exception in thread "main" java.lang.ArithmeticException: / by zero
at ExpEx.main(ExpEx.java:8)
Explanation:
In the above program, we created a class ExpEx class that contains the main() method. Here, we created try and finally block. The try block generated divide by zero exception. But we also used finally block, and as we know that finally will always execute after try block if it is defined.
Question 3:
public class ExpEx {
public static void main(String[] args) {
try {
int num1 = 10;
int num2 = 0;
int num3 = 0;
num3 = num1 / num2;
System.out.println("Result: " + num3);
}
catch(ArithmeticException e) {
System.out.println(e);
}
}
}
Output:
java.lang.ArithmeticException: / by zero
Explanation:
In the above program, we created a class ExpEx class that contains the main() method. Here, we created try and catch block.
The try block generated divide by zero exception because here we divided the local variable num1 by num2 and num2 contains value 0. Then generated exception caught by catch block. Then we print the exception object on the console screen.
Question 4:
public class ExpEx {
public static void main(String[] args) {
try {
int num1 = 10;
int num2 = 0;
int num3 = 0;
num3 = num1 / num2;
System.out.println("Result: " + num3);
}
catch(Exception e) {
System.out.println(e);
}
catch(ArithmeticException e) {
System.out.println(e);
}
}
}
Output:
/ExpEx.java:15: error: exception ArithmeticException has already been caught
catch(ArithmeticException e) {
^
1 error
Explanation:
The above program will generate syntax error because here we used two catch blocks but "catch (Exception e)" will catch all type exception because Exception class is the superclass for all type of exceptions in Java, but we also define "catch (ArithmeticException e)" block, which is not required. That's why the above program will generate a syntax error.
Question 5:
public class ExpEx {
public static void main(String[] args) {
try {
int num1 = 50;
int num2 = 0;
int num3 = 0;
num3 = num1 / num2;
System.out.println("Result: " + num3);
}
catch(ArithmeticException e) {
System.out.println(e);
}
catch(Exception e) {
System.out.println(e);
}
}
}
Output:
java.lang.ArithmeticException: / by zero
Explanation:
In the above program, we declared three local variables num1, num2, and num3 initialized with 50, 0, and 0 respectively.
num3 = num1/num2;
num3 = 50/0;
The above statement will generate a divide by zero exception that will be caught by the "catch (ArithmeticException e)" block. Then we printed the exception using object 'e' on the console screen.
Here, we also defined the "catch (Exception e)" block, which is not required because the above code will generate only divide by zero exception.