Chained Exception in java

Chained Exception in java
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.

Chained Exception

Chained Exception was added to Java in JDK 1.4. This feature allows you to relate one exception with another exception, i.e one exception describes a cause of another exception. For example, consider a situation in which a method throws an ArithmeticException because of an attempt to divide by zero but the actual cause of exception was an I/O error which caused the divisor to be zero. The method will throw only ArithmeticException to the caller. So the caller would not come to know about the actual cause of an exception. Chained Exception is used in such type of situations.
Chained Exception in java

Two new constructors and two methods were added to the Throwable class to support the chained exception.
  1. ThrowableThrowable cause )
  2. ThrowableString strThrowable cause )
In the first form, the parameter cause specifies the actual cause of an exception. In the second form, it allows us to add an exception description in string form with the actual cause of an exception.
getCause() and initCause() are the two methods added to Throwable class.
  • getCause() method returns the actual cause associated with the current exception.
  • initCause() set an underlying cause(exception) with invoking exception.

Example

import java.io.IOException;
public class ChainedException 
 {
  public static void divide(int a, int b)
  {
   if(b==0)
   {
    ArithmeticException ae = new ArithmeticException("top layer");
    ae.initCause( new IOException("cause") );
    throw ae;
   }
   else
   {
    System.out.println(a/b);
   }
  }

 public static void main(String[] args)
 {
  try {
   divide(5, 0);
  } 
  catch(ArithmeticException ae) {
   System.out.println( "caught : " +ae);
   System.out.println("actual cause: "+ae.getCause());
  }
 }
}
Output :
caught:java.lang.ArithmeticException: top layer
actual cause: java.io.IOException: cause

Introduction to Multithreading

A program can be divided into a number of small processes. Each small process can be addressed as a single thread (a lightweight process). Multithreaded programs contain two or more threads that can run concurrently. This means that a single program can perform two or more tasks simultaneously. For example, one thread is writing content on a file at the same time another thread is performing spelling check.
Introduction to Multithreading in java

In Java, the word thread means two different things.
  • An instance of Thread class.
  • or, A thread of execution.
An instance of Thread class is just an object, like any other object in java. But a thread of execution means an individual "lightweight" process that has its own call stack. In Java, each thread has its own call stack.
Introduction to Multithreading in java













The main thread

Even if you don't create any thread in your program, a thread called main thread is still created. Although the main thread is automatically created, you can control it by obtaining a reference to it by calling currentThread() method.
Two important things to know about the main thread are,
  • It is the thread from which other threads will be produced.
  • main thread must be always the last thread to finish execution.
class MainThread
{
 public static void main(String[] args)
 {
  Thread t=Thread.currentThread();
  t.setName("MainThread");
  System.out.println("Name of thread is "+t);
 }
}
Output :
Name of thread is Thread[MainThread,5,main]
The main thread
















  1. New: A thread begins its life cycle in the new state. It remains in this state until the start() method is called on it.
  2. Runnable: After invocation of start() method on a new thread, the thread becomes unable.
  3. Running: A method is in running thread if the thread scheduler has selected it.
  4. Waiting: A thread is waiting for another thread to perform a task. In this stage, the thread is still alive.
  5. Terminated: A thread enters the terminated state when it completes its task.

Thread Priorities

Every thread has a priority that helps the operating system determine the order in which threads are scheduled for execution. In Java thread priority ranges between,
  • MIN-PRIORITY (a constant of 1)
  • MAX-PRIORITY (a constant of 10)
By default, every thread is given a NORM-PRIORITY(5). The main thread always has NORM-PRIORITY.
























Comments