Chained Exception was added to Java in System.out.
println(a/b);
JDK 1.4. This feature allows you to relate }
one exception with another exception, i.e }
one exception describes cause of another
exception. For example, consider a public static void main(String[] args)
situation in which a method throws {
an ArithmeticException because of an try
attempt to divide by zero but the actual {
cause of exception was an I/O error which divide(5, 0);
caused the divisor to be zero. The method }
will throw only ArithmeticException to catch(ArithmeticException ae) {
the caller. So the caller would not come to System.out.println( "caught : " +ae);
know about the actual cause of exception. System.out.println("actual cause:
Chained Exception is used in such type of "+ae.getCause());
situations. }
Two new constructors and two new }
methods were added to Throwable class }
to support chained exception. NumberFormatException was thrown but
1. Throwable(Throwable cause) the actual cause of exception was a null
2. Throwable(String str, Throwable pointer exception.
cause)
In the first constructor, the public class ChainedDemo1
paramter cause specifies the actual cause {
of exception. In the second form, it allows public static void main(String[] args)
us to add an exception description in string {
form with the actual cause of exception. try
getCause() and initCause() are the two {
methods added to Throwable class.
getCause() method returns the NumberFormatException a = new
actual cause associated with current NumberFormatException("====>
exception. Exception");
initCause() set an underlying
cause(exception) with invoking a.initCause(new
exception. NullPointerException("====> Actual
ArithmeticException was thrown by the cause of the exception"));
program but the real cause of exception
was IOException. We set the cause of throw a;
exception using initCause() method. }
import java.io.IOException;
catch(NumberFormatException a)
public class ChainedException {
{
public static void divide(int a, int b) System.out.println(a);
{ System.out.println(a.getCause());
if(b == 0) }
{ }
ArithmeticException ae = new }
ArithmeticException("top layer");
ae.initCause(new Exception propagation
IOException("cause")); In Java, an exception is thrown from the
throw ae; top of the stack, if the exception is not
} caught it is put in the bottom of the stack,
else this process continues until it reaches to
{ the bottom of the stack and caught. It is
known as exception propagation. By try {
default, an unchecked exception is n1();
forwarded in the called chain. }
an exception occurred in method a1 which catch (Exception e)
is called by method a2 and a2 is called by {
method a3. Method a3() is enclosed in try System.out.println("Exception
block to provide the safe guard. We know handled");
exception will be thrown be method a1 but }
handled in method a3(). This is called }
exception propagation. public static void main(String args[])
{
class ExpDemo1{ Demo obj = new Demo();
void a1() obj.p1();
{ }
int data = 30 / 0; }
} Creating a thread in Java
void a2() o implement multithreading, Java defines
{ two ways by which a thread can be
a1(); created.
} By implementing
void a3() the Runnable interface.
{ By extending the Thread class.
try { mplementing the Runnable Interface
a2(); The easiest way to create a thread is to
} create a class that implements the runnable
catch (Exception e) interface. After implementing runnable
{ interface, the class needs to implement
System.out.println(e); the run() method.
} Run Method Syntax:
} public void run()
It introduces a concurrent thread into your
public static void main(String args[]) program. This thread will end when run()
{ method terminates.
ExpDemo1 obj1 = new ExpDemo1(); You must specify the code that your thread
obj1.a3(); will execute inside run() method.
} run() method can call other methods, can
} use other classes and declare variables just
an IOException from method m1() which like any other normal method.
is called inside the n1(). Exception thrown class MyThread implements Runnable
by method m1() is handled by method {
n1(). public void run()
{
import java.io.IOException; System.out.println("concurrent thread
class Demo{ started running..");
void m1() throws IOException }
{ }
throw new IOException("device error");
} class MyThreadDemo
void n1() throws IOException {
{ public static void main(String args[])
m1(); {
} MyThread mt = new MyThread();
void p1() Thread t = new Thread(mt);
{ t.start();
} Thread.sleep(1000);
} }
To call the run()method, start() method is }
used. On calling start(), a new stack is catch(InterruptedException e)
provided to the thread and run() method is {
called to introduce the new thread into the System.out.println("Child Thread
program. Interrupted");
implementing Runnable interface in your }
class, then you need to explicitly create a System.out.println("Child Thread
Thread class object and need to pass the Exiting");
Runnable interface implemented class }
object as a parameter in its constructor. }
Extending Thread class class MainThread
This is another way to create a thread by a {
new class that extends Thread class and public static void main(String [] args)
create an instance of that class. The {
extending class must NewThread nt = new NewThread();
override run() method which is the entry nt.t.start();
point of new thread. System.out.println("Main Thread
class MyThread extends Thread Started");
{ try
public void run() {
{ for(int i=10; i>=1; i--)
{
System.out.println("concurrent System.out.println("Main:"+i);
thread started running.."); Thread.sleep(1000);
} }
} }
catch(InterruptedException e)
classMyThreadDemo {
{ System.out.println("Main Thread
public static void main(String Interrupted");
args[]) }
{ System.out.println("Main Thread
MyThread mt = new Exiting");
MyThread(); }
mt.start(); }
}
}
class NewThread implements Runnable
{
Thread t;
NewThread()
{
t = new Thread(this,"Child");
System.out.println("Child Thread"+t);
}
public void run()
{
try
{
for(int i=1; i<=10; i++)
{
System.out.println("Child:"+i);