Q:

What is the output of the following program? exception handling in java

0

What is the output of the following programs? exception handling in java

=======================

program 1:

class JavaException {
public static void main(String args[]) {
 int d = 0;
 int n = 20;
 try {
 int fraction = n / d;
 System.out.println("after division");
 } catch (ArithmeticException e) {
 System.out.println(e);
 }
 System.out.println("End Of Main");
}
}

 

program 2:

class Test {
public static void main(String args[]) {
Scanner read = new Scanner(System.in);
int num1, num2;
System.out.println("Enter two numbers");
try {
 num1 = read.nextInt();
 num2 = read.nextInt();
 System.out.println("Result = " + (num1/num2));
 }
catch (InputMismatchException inpEx) {
 System.out.println("Input error : "+inpEx); 
 } 
finally {
 System.out.println("Exception Example ");
 } 
 }
}

 

program 3:

class Exceptions {
public static void main(String[] args) {
 int languages[] = { 1, 2, 3};
 
try {
 for (int c = 1; c <= 3; c++) {
 System.out.println(languages[c]);
 }
 }
catch(InputMismatchException e){ 
 System.out.println ("first Catch"); 
 } 
 catch (Exception e) {
 System.out.println("Exception Catch");
 } 
}
}

 

program 4:

public class Final {
 public static void main(String[] args)
 {
 try
 {
 System.out.println("First");
 int value = 10 / 0;
 throw new NullPointerException();
 }
 catch(NullPointerException e)
 {
 
System.out.println("NullPointerException");
 }
 catch(ArithmeticException e)
 {
 System.out.println("ArithmeticException");
 }
 catch(InputMismatchException e)
 { System.out.println("InputMismatchException");
 }
 finally
 {
 System.out.println("Finally");
 }
}}

 

program 5:

public class TestClass
{
 public static void main(String[] args) {
 try {
 System.out.printf("%d ", 1);
 TestClass [] t = new TestClass[3];
 System.out.println(t[1].toString());
 }
 catch (IndexOutOfBoundsException e) {
 System.out.printf("%d ", 2);
 }
 catch(NullPointerException e) {
 System.out.printf("%d ", 3);
 }
 catch(ArithmeticException e) {
 System.out.printf("%d ", 4);
 }
 Finally {
 System.out.printf("%d ", 5);
 }
 }}

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

program 1:

=========

• In case that the user enters 10 and Test

• In case that the user enters 15 and 3

 

program 2:

 

program 3:

 

program 4:

 

program 5:

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now