1.Write code to generate and catch an ArrayIndexOutOfBoundsException 2.Create yo
ID: 3830776 • Letter: 1
Question
1.Write code to generate and catch an ArrayIndexOutOfBoundsException 2.Create your own exception class using the extends keyword. Write a constructor for this class that takes a String argument and stores it inside the object with a String reference. Write a method that prints out the stored String. Create a try-catch clause to exercise your new exception 3.Create a three-level hierarchy of exceptions. Now create a base-class A with a method that throws an exception at the base of your hierarchy. Inherit B from A and override the method so it throws an exception at level two of your hierarchy. Repeat by inheriting class C from B. In main( ), create a C and upcast it to A, then call the method 4.Create three new types of exceptions. Write a class with a method that throws all three. In main( ), call the method but only use a single catch clause that will catch all three types of exceptions 5.Modify Exercise 4 by adding a finally clause. Verify that your finally clause is executed, even if a NullPointerException is thrown. 6.Write code to generate and catch a runtime exception
Explanation / Answer
1.
class Exception1
{
public static void main(String args[])
{
try
{
int arr[]=new int[4];
a[0]=1;
a[1]=2;
a[2]=3;
a[3]=4;
a[4]=5;
a[5]=6;
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Error Detected"+e.getMessage());
}
}
}
2.
class UsExcep extends Exception
{
UsExcep(String msg)
{
super(msg);
}
}
class ExceptT
{
void display(int x) throws UsExcep
{
System.out.println("value of x is="+x);
throw new UsExcep("New Exception");
}
}
class Exception2
{
public static void main(String args[])
{
ExceptT ob1=new ExceptT();
try{
ob1.display(30);
}
catch(UserException e)
{
System.out.println("New Error found"+e.getMessage());
}
}
}
3.
class L1 extends Exception {}
class L2 extends L1 {}
class L3 extends L2 {}
class A {
void fn() throws L1 {
throw new L1();
}
}
class B extends A {
void fn() throws L2 {
throw new L2();
}
}
class C extends B {
void fn() throws L3 {
throw new L3();
}
}
public class Exception3 {
public static void main(String[] args) {
A a = new C();
try {
a.f();
} catch(L3 e3) {
System.out.println("Detected e3");
} catch(L2 e2) {
System.out.println("Detected e2");
} catch(L1 e1) {
System.out.println("Detected e1");
}
}
}
4.
import java.io.FileNotFoundException;
import java.io.IOException;
public class Exception4 {
public static void main(String[] args) throws FileNotFoundException, IOException {
try{
testEx(-3);
testEx(-9);
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
System.out.println("Fre resources");
}
testEx(15);
}
public static void testEx(int i) throws FileNotFoundException, IOException{
if(i < 0){
FileNotFoundException myEx = new FileNotFoundException("Negative Integer "+i);
throw myEx;
}else if(i > 10){
throw new IOException("value satisfied from 0 to 10");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.