51 Program to Demonstrate Exception
{
static void meth1()
{
int d=0;
int a=10/d;
}
public static void main(String a[])
{
Excep.meth1();
}
}
OUTPUT :
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Excep.meth1(Excep.java:6)
at Excep.main(Excep.java:10)
52 Program to Demonstrate TRY CATCH
class TC
{
public static void main(String a[])
{
int c[]={2,4,6,8};
try
{
c[34]=99;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array Index Out Of Bound "+e);
}
}
}
OUTPUT :
Array Index Out Of Bound java.lang.ArrayIndexOutOfBoundsException: 34
53 Program to CATCH ARITHMETIC Exceptions
{
public static void main(String args[])
{
int d,a;
try
{
d = 0;
a = 42/d;
System.out.println("This will not Display");
}
catch(ArithmeticException e)
{
System.out.println("Division by zero");
}
System.out.println("After try/Catch Statements");
}
}
OUTPUT :
Division by zero
After try/Catch Statements
54 Program to demonstrate Multiple CATCH
class trydemo1
{
public static void main(String args[])
{
try {
int l=args.length;
System.out.println("l = " +l);
int b = 34/l;
int c[]={0};
c[34]=100;
System.out.println("End of Try Block");
}
catch(ArithmeticException e)
{
System.out.println("Please Enter Command line Arguments");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Your Array size is small");
}
catch(Exception e)
{
System.out.println("Other type of Exception");
}
}}
OUTPUT :
l = 0
Please Enter Command line Arguments
55 Program to demonstrate NESTED TRY
class NestTry
{
public static void main(String args[])
{
try {
int a = args.length;
int b = 42 /a;
System.out.println("a = "+ a);
try {
if(a==1) a = a/(a-a);
if (a==2) {
int c[] = {1};
c[42] = 99;
}
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Array index out-of-bounds:"+e);
}
} catch(ArithmeticException e) {
System.out.println("Divide by 0:"+e);
}
}
}
OUTPUT :
Divide by 0:java.lang.ArithmeticException: / by zero
56 Program to demonstrate FINALLY clause
class trydemo{
public static void main(String args[]){
try{
int l=args.length;
System.out.println("l = " +l);
int b = 34/l;
int c[]={0};
c[34]=100;
System.out.println("End of Try Block");
}
catch(ArithmeticException e)
{
System.out.println("Please Enter Command line Arguments");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Your Array size is small");
}
catch(Exception e)
{
System.out.println("Other type of Exception");
}
finally{
System.out.println("This will be executed");
}
}}
OUTPUT :
l = 0
Please Enter Command line Arguments
This will be executed
57 Program to Demonstrate User Defined EXCEPTIONS
class Myexception extends Exception {
private int d;
Myexception(int a)
{
d=a;
}
public String toString() {
return "Myexception["+d+"]";
}}
class UserExcep {
static void compute(int a) throws Myexception {
System.out.println("called compute("+a+")");
if(a>50)
throw new Myexception(a);
System.out.println("normal exit");
}
public static void main(String a[])
{
try {
compute(34);
compute(99);
}
catch(Myexception e)
{
System.out.println("caught "+e);
}}}
OUTPUT :
called compute(34)
normal exit
called compute(99)
caught Myexception[99]
class ThrowsDemo
{
static void throwOne() throws IllegalAccessException
{
System.out.println("Inside throwOne.");
throw new IllegalAccessException("demo");
}
public static void main(String args[])
{
try
{
throwOne();
}
catch (IllegalAccessException e)
{
System.out.println("Caught "+e);
}
}
}
OUTPUT :
Inside throwOne.
Caught java.lang.IllegalAccessException: demo
59 Program to demonstrate MULTITHREADING
class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
System.out.println("\tfrom Thread A : i = " +i);
System.out.println("Exit from A");
}}
class B extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
System.out.println("\tfrom Thread B : j = " +j);
System.out.println("Exit from B");
}}
class C extends Thread
{
public void run()
{
for(int k=1;k<=5;k++)
System.out.println("\tfrom Thread C : k = " +k);
System.out.println("Exit from C");
}}
class Threadtest
{
public static void main(String args[])
{
new A().start();
new B().start();
new C().start();
try{
Thread.sleep(10);
}
catch(InterruptedException e){
System.out.println("Exception " +e);
}
}
}
OUTPUT :
from Thread A : i = 1
from Thread A : i = 2
from Thread A : i = 3
from Thread A : i = 4
from Thread A : i = 5
Exit from A
from Thread B : j = 1
from Thread C : k = 1
from Thread B : j = 2
from Thread C : k = 2
from Thread B : j = 3
from Thread C : k = 3
from Thread B : j = 4
from Thread C : k = 4
from Thread B : j = 5
from Thread C : k = 5
Exit from B
Exit from C
60 Program to Demonstrate Runnable Interface
class A implements Runnable
{
public void run()
{
for(int i=1;i<=5;i++)
System.out.println("\tfrom Thread A : i = " +i);
System.out.println("Exit from A");
}}
class B implements Runnable
{
public void run()
{
for(int j=1;j<=5;j++)
System.out.println("\tfrom Thread B : j = " +j);
System.out.println("Exit from B");
}}
class C implements Runnable{
public void run(){
for(int k=1;k<=5;k++)
System.out.println("\tfrom Thread C : k = " +k);
System.out.println("Exit from C");
}}
public static void main(String args[]){
A a = new A();
B b = new B();
C c = new C();
Thread t1 = new Thread(a);
t1.start();
Thread t2 = new Thread(b);
t2.start();
Thread t3 = new Thread(c);
t3.start();
try{
Thread.sleep(10);
}
catch(InterruptedException e){
System.out.println("Exception " +e);
}}}
OUTPUT :
from Thread A : i = 1
from Thread B : j = 1
from Thread C : k = 1
from Thread A : i = 2
from Thread B : j = 2
from Thread C : k = 2
from Thread A : i = 3
from Thread B : j = 3
from Thread C : k = 3
from Thread A : i = 4
from Thread B : j = 4
from Thread C : k = 4
from Thread A : i = 5
from Thread B : j = 5
from Thread C : k = 5
Exit from A
Exit from B
Exit from C
0 comments:
Post a Comment