0% found this document useful (0 votes)
58 views

Q.Write A Program For Multiple Catch To Fire Arrayindexoutofboundsexception and Stringindexoutofboundsexception Both

The document contains 4 code examples that demonstrate how to handle different exceptions in Java: 1. A program that catches both ArrayIndexOutOfBoundsException and StringIndexOutOfBoundsException using multiple catch blocks. 2. A program that catches ArithmeticException when dividing a number by zero. 3. A program that catches a NullPointerException when calling a method on a null object reference. 4. A program that generates a NegativeArraySizeException by creating an array with a negative size.

Uploaded by

Anirban Nandi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Q.Write A Program For Multiple Catch To Fire Arrayindexoutofboundsexception and Stringindexoutofboundsexception Both

The document contains 4 code examples that demonstrate how to handle different exceptions in Java: 1. A program that catches both ArrayIndexOutOfBoundsException and StringIndexOutOfBoundsException using multiple catch blocks. 2. A program that catches ArithmeticException when dividing a number by zero. 3. A program that catches a NullPointerException when calling a method on a null object reference. 4. A program that generates a NegativeArraySizeException by creating an array with a negative size.

Uploaded by

Anirban Nandi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Q.

Write a program for multiple catch to fire ArrayIndexOutOfBoundsException and


StringIndexOutOfBoundsException both.

ANS::-

class MultiCatch

public static void main(String str[])

String a="Hello";

int b[]={1,2};

try

char c=a.charAt(10);

System.out.println(c);

for(int i=0;i<3;i++)

System.out.printf("%d\n",b[i]);

catch(StringIndexOutOfBoundsException e)

System.out.println(e);

catch(ArrayIndexOutOfBoundsException e)

System.out.println(e);

System.out.println("After Try-Catch Blocks");

}
Q.Write a program to handle the ArithmeticException.

class Example1

public static void main(String args[])

try{

int num1=30, num2=0;

int output=num1/num2;

System.out.println ("Result: "+output);

catch(ArithmeticException e){

System.out.println ("You Shouldn't divide a number by zero");

Q. Define an object reference and initialize it to null. Try to call a method through
this reference. Now wrap the code in a try-catch clause to catch the exception.
class NULL

public static void main(String str[])

NULL ob=null;

try

ob.go();

catch(Exception e)

System.out.println(e);

System.out.println("NORMAL FLOW....");

System.out.println("Rest Of The Code.....");


}

void go()

System.out.println("HELLO WORLD");

Q.Write a program to fire the NegativeArraySize exception.


class Negative

public static void main(String str[])

int[] arr=new int[-10];

You might also like