WEEK 7
AIM: a) Write a java program that describes exception handling mechanism.
PROGRAM:
public class Main {
static void checkAge(int age) {
if (age < 18) {
throw new ArithmeticException("Access denied - You must be at least
18 years old.");
} else {
System.out.println("Access granted - You are old enough!");
public static void main(String[] args) {
checkAge(15);
OUTPUT:
Access denied - You must be at least 18 years old
WEEK 7
AIM: b) Write a java program illustrating multiple catch clauses.
PROGRAM:
public class MultipleCatchBlock {
public static void main(String[] args) {
try{
int a[ ]=new int[5];
System.out.println(a[10]);
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception
occurs");
}
catch(Exception e)
{
System.out.println("Parent Exception occurs");
}
System.out.println("rest of the code");
}
}
OUTPUT:
ArrayIndexOutOfBounds Exception occurs
rest of the code
WEEK 8
AIM: a) Write a java program for creation of Java Built-in Exceptions.
PROGRAM:
// Java program to demonstrate
// ArithmeticException
class ArithmeticException_Demo {
public static void main(String args[])
try {
int a = 30, b = 0;
int c = a / b; // cannot divide by zero
System.out.println("Result = " + c);
catch (ArithmeticException e) {
System.out.println("Can't divide a number by 0");
OUTPUT: Can't divide a number by 0
// Java program to demonstrate
// ArrayIndexOutOfBoundException
class ArrayIndexOutOfBound_Demo {
public static void main(String args[])
try {
int a[] = new int[5];
a[6] = 9; // accessing 7th element in an array of
// size 5
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array Index is Out Of Bounds");
Output:
Array Index is Out Of Bounds
// Java program to illustrate the
// concept of ClassNotFoundException
class A {
class B {
class MyClass {
public static void main(String[ ] args)
Object o = class.forName(args[0]).newInstance( );
System.out.println("Class created for" + o.getClass().getName( ));
Output: ClassNotFoundException
// Java program to demonstrate NullPointerException
class NullPointer_Demo {
public static void main(String args[])
{
try {
String a = null; // null value
System.out.println(a.charAt(0));
}
catch (NullPointerException e) {
System.out.println("NullPointerException..");
}
}
}
Output : NullPointerException..
WEEK 8
AIM: b) Write a java program for illustrating the property of “throw”
keyword.
PROGRAM:
class Test
{
static void check() throws ArithmeticException
{
System.out.println("Inside check function");
throw new ArithmeticException("demo");
}
public static void main(String args[])
{
try
{
check();
}
catch(ArithmeticException e)
{
System.out.println("caught" + e);
}
}
}
OUTPUT:
Inside check function
caughtjava.lang.ArithmeticException: demo
WEEK 9
AIM: a) Write a java program for illustrating the property of “finally”
keyword.
PROGRAM:
class Main
{
public static void main(String[] args)
{
int a[] = new int[2];
try
{
System.out.println("Access invalid element"+ a[3]);
/* the above statement will throw ArrayIndexOutOfBoundException */
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Exception caught");
}
finally
{
System.out.println("finally is always executed.");
}
}
}
OUTPUT:
Exception caught.
finally is always executed.
WEEK 9
AIM: b) Write a java program for creation of User Defined Exception.
PROGRAM:
class JavaException{
public static void main(String args[]){
try{
throw new MyException(2);
// throw is used to create a new exception and throw it.
}
catch(MyException e){
System.out.println(e) ;
}
}
}
class MyException extends Exception{
int a;
MyException(int b) {
a=b;
}
public String toString(){
return ("Exception Number = "+a) ;
}
}
OUTPUT:
Exception Number = 2