WEEK 7 To WEEK 9 JAVA LAB PROGRAM
WEEK 7 To WEEK 9 JAVA LAB PROGRAM
PROGRAM:
} else {
checkAge(15);
OUTPUT:
PROGRAM:
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:
PROGRAM:
// ArithmeticException
class ArithmeticException_Demo {
try {
int a = 30, b = 0;
catch (ArithmeticException e) {
// ArrayIndexOutOfBoundException
class ArrayIndexOutOfBound_Demo {
public static void main(String args[])
try {
// size 5
catch (ArrayIndexOutOfBoundsException e) {
Output:
// concept of ClassNotFoundException
class A {
class B {
class MyClass {
public static void main(String[ ] args)
Object o = class.forName(args[0]).newInstance( );
Output: ClassNotFoundException
PROGRAM:
class Test
{
static void check() throws ArithmeticException
{
System.out.println("Inside check function");
throw new ArithmeticException("demo");
}
OUTPUT:
caughtjava.lang.ArithmeticException: demo
WEEK 9
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.
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