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

(Object Oriented Programming) : CCS0023L

This document discusses how to implement exception handling in Java by catching ArrayIndexOutOfBoundsException, NullPointerException, and RuntimeException. It provides code samples of a FindArray class that throws each exception type in the main method. Each code sample catches the specific exception and prints the exception message to the output.

Uploaded by

Rhea Kim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

(Object Oriented Programming) : CCS0023L

This document discusses how to implement exception handling in Java by catching ArrayIndexOutOfBoundsException, NullPointerException, and RuntimeException. It provides code samples of a FindArray class that throws each exception type in the main method. Each code sample catches the specific exception and prints the exception message to the output.

Uploaded by

Rhea Kim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

COLLEGE OF COMPUTER STUDEIS

INFORMATION TECHNOLOGY DEPARTMENT

CCS0023L
(Object Oriented Programming)

Machine Problem

8
How to implement exception and assertion
I.

Create a Class FindArray that will try to iterate in the array displaying the content
in each index. The program should be able to catch an
ArrayIndexOutOfBoundsException, a NullPointerException and a
RunTimeException.

NullPointerException
package com.company;

public class FindArray {

public static void main(String[] args) {

try {
int[] a = null;
System.out.println(a[3]);
}
catch(NullPointerException e)
{
System.out.println("The exception is " + e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("The exception is " + e);
}
catch(RuntimeException e)
{
System.out.println("The exception is " + e);
}
}
}
SAMPLE OUTPUTS(NullPointerException)

ArrayIndexOutOfBoundsException
package com.company;

public class FindArray {

public static void main(String[] args) {

try {
int[] a = {1,2,3};
System.out.println(a[3]);
}
catch(NullPointerException e)
{
System.out.println("The exception is " + e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("The exception is " + e);
}
catch(RuntimeException e)
{
System.out.println("The exception is " + e);
}
}
}
SAMPLE OUTPUTS(ArrayIndexOutOfBoundsException)
RunTimeException
package com.company;

public class FindArray {

public static void main(String[] args) {

try {
throw new RuntimeException("Hello");
}
catch(NullPointerException e)
{
System.out.println("The exception is " + e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("The exception is " + e);
}
catch(RuntimeException e)
{
System.out.println("The exception is " + e);
}
}
}
SAMPLE OUTPUTS(RunTimeException)

You might also like