Unit Iii
Unit Iii
Exceptions in Java
Exception Hierarchy
All exception and error types are subclasses of class Throwable, which is the base class of
hierarchy.
Error:
An Error indicates serious problem that a reasonable application should not try to catch.Error is
irrecoverable
Exception:
Ø checked Exceptions
Ø unchecked Exception
Ø Checked Exception:
The classes which are directly inherited from Throwable class ,which are checked at
Compilation Process are known as checked exceptions.
Checked exceptions are checked at compile-time.
for e.g. IOException, SQLException etc.
Ø Unchecked Exception
· The classes which inherit from Throwable classes ,which occur at Runtime(Execution)
Process are known as unchecked exceptions.
· Unchecked exceptions are not occurred at checked at compile-time, but they are checked
at runtime are also known as Runtime Exceptions
for e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.
Ø The java.lang package defines several classes and exceptions.
Some of these classes are unchecked while some other classes are checked.
Exception Handling
Exception handling is a framework that is used to handle runtime errors only, compile time
errors are not handled by exception handling in java.
We use specific keywords in the java program to create an exception handler block.
Exception Handling Keywords
Java exception handling is managed via five keywords: try, catch, throw, throws, and finally.
finally The "finally" block is used to execute the important code of the
program. It is executed whether an exception is handled or not.
Ø When an Exception is thrown , Control is transferred to the catch block that handles the error.
Ø The Programmer now has two Choices, that he can print the error message and exit from the
program.this technique is called as Termination Model.
Ø The user can also continue Execution by calling some other function after printing the error
message. This Technique is called the Resumptive Model.
If Exception handling is not Provided , then in a Java application the program stops
automatically after printing the exception message in try-catch block and also continues the code
after exception occurs by using try-catch block.
For Example:
Class test
Int a=10,b=0,c;
try
c=a/b;
System.out.println(“result=”+c);
catch (ArthematicException e)
System.out.println(“divide by Zero”);
}
Uncaught exceptions:
If Exception handling is not Provided , then in a java application the program terminates
automatically after printing the default exception message.
class Exc0 {
int d = 0;
int a = 42 / d;
In this example, we haven’t supplied any exception handlers of our own, so the exception is
caught by the default handler provided by the Java run-time System.
Any exception that is not caught by your program will ultimately be processed by the default
handler. The default handler displays a string describing the exception, prints a stack trace from
the point at which the exception occurred, and terminates the program.
Output:
java.lang.ArithmeticException: / by zero
at Exc0.main(Exc0.java:4)
The stack trace will always show the sequence of method invocations that led up to the error.
Using try and catch
· Although the default exception handler provided by the Java run-time system is useful for
debugging, you will usually want to handle an exception yourself.
· First, it allows you to fix the error. Second, it prevents the program from automatically
terminating.
· To guard against and handle a run-time error, simply enclose the code that you want to
monitor inside a try block. Immediately following the try block, include a catch clause that
specifies the exception type that you wish to catch.
· To illustrate how easily this can be done, the following program includes a try block and
a catch clause that processes the ArithmeticExceptiongenerated by the division-by-zero error:
Example:
class Exc2 {
int d, a;
d = 0;
a = 42 / d;
catch (ArithmeticException e)
System.out.println("Division by zero.");
}
System.out.println("After try-catch statement.");
Output:
Division by zero.
Notice that the call to println( ) inside the try block is never executed. Once an exception is
thrown, program control transfers out of the try block into the catch block.
Put differently,catch is not “called,” so execution never “returns” to the try block from a catch.
Thus, the line “This will not be printed.” is not displayed.
Once the catch statement has been executed, program control continues with the next line in the
program following the entire try/catch mechanism.
In some cases, more than one exception could be raised by a single piece of code. To
handle this type of situation, you can specify two or more catch clauses, each catching a
different type of exception.
When an exception is thrown, each catch statement is inspected in order, and the first one whose
type matches that of the exception is executed. After one catch statement executes, the others are
bypassed, and execution continues after the try/catch block.
class MultiCatch {
try {
int a = args.length;
System.out.println("a = " + a);
int b = 42 / a;
int c[] = { 1 };
c[42] = 99;
catch(ArithmeticException e) {
catch(ArrayIndexOutOfBoundsException e) {
C:\>java MultiCatch
a=0
a=1
Example2:
/* This program contains an error. A subclass must come before its superclass in a series of catch
statements. If not, unreachable code will be created and a
*/
class SuperSubCatch {
try {
int a = 0;
int b = 42 / a;
} catch(Exception e) {
}
}
If you try to compile this program, you will receive an error message stating that the
second catch statement is unreachable because the exception has already been caught. Since
ArithmeticExceptionis a subclass of Exception, the first catch statement will handle all
Exception-based errors, including ArithmeticException. This means that the second catch
statement will never execute. To fix the problem, reverse the order of the catch statements.
The try statement can be nested. That is, a try statement can be inside the block of
another try. Each time a try statement is entered, the context of that exception is pushed on the
stack.
If an inner try statement does not have a catch handler for a particular exception, the
stack is unwound and the next try statement’s catch handlers are inspected for a match.
This continues until one of the catch statements succeeds, or until all of the nested try
statements are exhausted.
If no catch statement matches, then the Java run-time system will handle the exception.
Here is an example that uses nested try statements:
class NestTry {
try {
int a = args.length;
int b = 42 / a;
System.out.println("a = " + a);
/* If one command-line arg is used, then a divide-by-zero exception will be generated by the
following code. */
if(a==2) {
int c[] = { 1 };
} catch(ArrayIndexOutOfBoundsException e) {
} catch(ArithmeticException e) {
As you can see, this program nests one try block within another. The program works as follows.
When you execute the program with no command-line arguments, a divide-by-zero
exception is generated by the outer try block.
If you execute the program with two command-line arguments, an array boundary
exception is generated from within the inner try block.
C:\>java NestTry
C:\>java NestTryOne
a=1
a=2
java.lang.ArrayIndexOutOfBoundsException:42
calls to methods. */
class MethNestTry {
if(a==2) {
int c[] = { 1 };
} catch(ArrayIndexOutOfBoundsException e) {
try {
int a = args.length;
a divide-by-zero exception. */
int b = 42 / a;
} catch(ArithmeticException e) {
}}
Using Throw:
Ø The throw keyword in Java is used to explicitly throw an exception from a method or
any block of code. We can throw either checked or unchecked exceptions.
Syntax:
Forexample:
Exception is a sub-class of Throwable and user defined exceptions typically extend Exception
class.
example:
//super(msg);
System.out.println(msg);
}
public class MyTest {
if(second==0){
return first/second;
System.out.println(divide(4,0));
Output of program:
C:\Roseindia\vinod\Exception>javac Test.java
C:\Roseindia\vinod\Exception>java Test
MyException: can't be divided by zero
at Test.divide(Test.java:10)
at Test.main(Test.java:15)
Using throws:
The Java throws keyword is used to declare an exception. It gives information to the
programmer that there may occur an exception so it is better for the programmer to provide the
exception handling code so that normal flow can be maintained.
This Exception Handling is mainly used to handle the checked exceptions. If there occurs
any unchecked exception such as NullPointerException, it is the programmer's fault that he is not
performing check up before the code being used.
Syntax:
//method code
Example:
import java.io.*;
class Test3{
public static void main(String args[]) throws FileNotFoundException,IOException
{
FileInputStreamfis=null;
fis = new FileInputStream (new File (args[0]));
intch;
while ((ch = fis.read()) != -1){
System.out.print ((char) ch);
}
fis.close();
}
}
output:
C:\Roseindia\vinod\Exception>javac Test3.java
C:\Roseindia\vinod\Exception>java Test3
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Test3.main(Test3.java:6)
Using finally
Java finally block is a block that is used to execute important code such as closing connection,
stream etc.
Java finally block is always executed whether exception is handled or not.
Example1 Example2
class TestFinallyBlock{
public static void main(String args[]){ import java.io.*;
try
{ class Test{
int data=25/0; public static void main(String args[]){
System.out.println(data); FileInputStreamfis=null;
} try {
catch(ArithmeticException e) fis = new FileInputStream (new File
{ System.out.println(e); } (args[0]));
intch;
finally while ((ch = fis.read()) != -1){
{ System.out.println("finally block is System.out.print ((char) ch);
always executed"); } }
}
System.out.println("rest of the code..."); catch (FileNotFoundException e){
} System.out.println("File not found!");
} }
Output: catch (IOException e){
java.lang.ArithmeticException: / by System.out.println("Unable to read file!");
zero }
finally block is always executed
rest of the code...
finally{
System.out.println();
System.out.println("In finally.");
try{
if(fis!=null){
fis.close();
}
}
catch (IOExceptionioe){
System.out.println("In finally.");
}
}
}
}
Output:
C:\Roseindia\vinod\Exception>javac Test.java
Inside the standard package java.lang, Java defines several exception classes.
The most general of these exceptions are subclasses of the standard type RuntimeException.
In the language of Java, these are called unchecked exceptions because the compiler does not
check to see if a method handles or throws these exceptions.
those exceptions defined by java.lang that must be included in a method’s throws list if that
method can generate one of these exceptions and does not handle it itself. These are called
checked exceptions.
Java defines several other types of exceptions that relate to its various class libraries.
If you are creating your own Exception that is known as a custom exception or user-defined
exception. Java custom exceptions are used to customize the exception according to user need.
package mypack;
InvalidAgeException(String s){
super(s);
System.out.println("not valid");
import java.io.*;
class TestCustomException1{
if(age<18)
else
System.out.println("welcome to vote");
try{
validate(13);
catch(Exception m)
} }
Output:
not valid
at excetions.myex.validate(myex.java:26)
at excetions.myex.main(myex.java:32)
Threads
What Is a Thread?
A single thread also has a beginning, an end, a sequence, and at any given time during the
runtime of the thread there is a single point of execution. However, a thread itself is not a
program. It cannot run on its own, but runs within a program.
A thread is considered lightweight because it runs within the context of a full-blown program
and takes advantage of the resources allocated for that program and the program's environment.
1. Process-based Multitasking
A process is an instance of a computer program that is executed sequentially. It is a
collection of instructions which are executed simultaneously at the rum time. Thus
several processes may be associated with the same program.
Example :– We can listen to music and browse the internet at the same time. The
processes in this example are the music player and browser.
2. Thread-based Multitasking.
A thread is a lightweight process which exists within a program and is executed to perform a
special task. Several threads of execution may be associated with a single process. Thus a
process that has only one thread is referred to as a single-threaded process, while a process with
multiple threads is referred to as a multi-threaded process.
Example: – Using a browser we can navigate through the webpage and at the same time
download a file. In this example, navigation is one thread and downloading is another
thread. Also in a word-processing application like MS Word, we can type text in one
thread and spell checker checks for mistakes in another thread.
and the text editor runs simultaneously. and a text editor at the same time.
tasks.
Thread multitasking has less Process multitasking has more overhead than thread
multitasking.
overhead than process multitasking.
It has faster data rate multithreading or It has slower data rate multitasking.
tasking
Threads share the same address space. Process requires its own address space.
One thread can pause without stopping other parts of your program. For example, the idle time
created when a thread reads data from a network or waits for user input can be utilized
elsewhere.
Multithreading allows animation loops to sleep for a second between each frame without
causing the whole system to pause. When a thread blocks in a Java program, only the single
thread that is blocked pauses. All other threads continue to run.
1. A thread can be running. It can be ready to run as soon as it gets CPU time.
3. A suspended thread can then be resumed, allowing it to pick up where it left off.
5. At any time, a thread can be terminated, which halts its execution immediately. Once
terminated, a thread cannot be resumed.
A thread can be in one of the five states. According to Sun, there are only 4 states in thread life
cycle in java: new, runnable, non-runnable and terminated. There is no running state.
The life cycle of the thread in java is controlled by JVM. The java thread states are as follows:
● New
● Runnable
● Running
● Non-Runnable (Blocked)
● Terminated
● New − A new thread begins its life cycle in the new state. It remains in this state until the
program starts the thread. It is also referred to as a born thread.
● Runnable − After a newly born thread is started, the thread becomes runnable. A thread
in this state is considered to be executing its task.
● Waiting − Sometimes, a thread transitions to the waiting state while the thread waits for
another thread to perform a task. A thread transitions back to the runnable state only
when another thread signals the waiting thread to continue executing.
● Timed Waiting (sleep) − A runnable thread can enter the timed waiting state for a
specified interval of time. A thread in this state transitions back to the runnable state
when that time interval expires or when the event it is waiting for occurs.
● Terminated (Dead) − A runnable thread enters the terminated state when it completes its
task or otherwise terminates.
Creating threads:
In Java, an object of the Thread class can represent a thread. Thread can be implemented through
any one of two ways:
1.Thread class:
Thread class provides constructors and methods to create and perform operations on a thread.
Thread class extends Object class and implements Runnable interface.
Constructors of Thread class
1. Thread()
This constructor creates a thread object having no predefined arguments.
2. Thread(String name)
This constructor creates a thread object having a String argument.
3. Thread(Runnable r)
This constructor creates a thread object having argument r of type runnable
4. Thread(Runnable r,String name)
This constructor creates a thread object having two arguments of String and Runnable type.
To create a thread is to create a new class that extends Thread, then override the run() method,
and then create an instance of that class. The run() method is what is executed by the thread
after you call start().
Here is an example of creating a Java Thread subclass:
publicclassMyClassextendsThread {
public void run(){
System.out.println("MyClass running");
}
}
To create and start the above thread you can do so like this:
MyClasst1=newMyClass ();
T1.start();
When the run() method executes it will print out the text " MyClass running ".
So far, we have been using only two threads: the main thread and one child thread. However,
our program can affect as many threads as it needs. Let's see how we can create multiple
threads.
Example: 2
Output:
Thread Name :main
Thread Name :My Thread 2
Thread Name :My Thread 1
Thread Name :My Thread 1
Thread Name :My Thread 2
Thread Name :My Thread 1
Thread Name :My Thread 2
Thread Name :My Thread 1
Thread Name :My Thread 2
Thread Name :My Thread 2
Thread Name :My Thread 1
Runnable interface:
The Runnable interface should be implemented by any class whose instances are intended to
be executed by a thread. Runnable interfaces have only one method named run().
To implement a Runnable interface, a class need only implement a single method called run( ),
which is declared like this:
Inside run( ), we will define the code that constitutes the new thread.
Example:
publicclassMyClassimplementsRunnable {
public void run(){
System.out.println("MyClass running");
}
}
To execute the run() method by a thread, pass an instance of MyClass to a Thread in its
constructor (A constructor in Java is a block of code similar to a method that's called when an
instance of an object is created). Here is how that is done:
Threadt1=newThread(newMyClass ());
t1.start();
When the thread is started it will call the run() method of the MyClass instance instead of
executing its own run() method. The above example would print out the text "MyClass running ".
Example2:
}
public void run() {
System.out.println(s);
}
}
}
}
Output: Thread started....
t.start();
}
Using sleep():
System.out.println(i);
}
}
t1.start();
t2.start();
}
}
Output:
1
1
2
2
3
3
4
4
As we know Java is a multithreading language which means JVM allows an application to have
multiple threads running concurrently. In Java each and every thread has priority, priority means
which decides the order of execution of that thread. Thread with higher priority will run first with
thread with lower priority and thread with equal priority will be treated the same by the scheduler
and they will follow the FCFS(First Cum First Serve) algorithm. We can also set the priority of
the thread by using setPriority() method as follows:
ThreadNamesetPriority(int number);
Here the number is an integer value between 1 to 10, here 1 is a minimum priority and 10 is the
maximum priority.
Thread class defines some priority constants as follows:
MIN_PRIORITY = 1
NORM_PRIORITY = 5
MAX_PRIORITY = 10;
In any thread, NORM_PRIORITY is the default one.
In Java, each and every thread has a priority which is defined by a number between, 1 to 10,
with 1 being the lowest priority,
10 being the highest priority,
A default priority for a thread is 5 but you can even set the priority for a thread.
Example:
synchronizing threads
When we start two or more threads within a program, there may be a situation when multiple
threads try to access the same resource and finally they can produce unforeseen results due to
concurrency issues. For example, if multiple threads try to write within the same file then they
may corrupt the data because one of the threads can override data, or while one line is opening
the same file at the same time another thread might be closing the same file.
So there is a need to synchronize the action of multiple threads and make sure that only one
thread can access the resource at a given point in time. This is implemented using a concept
called monitors. Each object in Java is associated with a monitor, which a thread can lock or
unlock. Only one thread at a time may hold a lock on a monitor.
The key to synchronization is the concept of the monitor (also called a semaphore). A monitor is
an object used as a mutually exclusive lock, or mutex. Only one thread can own a monitor at a
given time. When a thread acquires a lock, it is said to have entered the monitor.
All other threads attempting to enter the locked monitor will be suspended until the first thread
exits the monitor. These other threads are said to be waiting for the monitor. A thread that owns
a monitor can reenter the same monitor if it so desires.
Synchronization in Java is the capability to control the access of multiple threads to any shared
resource, by using synchronized blocks. You keep shared resources within this block. Following
is the general form of the synchronized statement −
Syntax
synchronized(objectidentifier) {
// Access shared variables and other shared resources
}
Here, the object identifier is a reference to an object whose lock is associated with the monitor
that the synchronized statement represents.
Java Synchronization is a better option where we want to allow only one thread to access the
shared resource.
The synchronization is mainly used to
To prevent thread interference.
To prevent consistency problems.
Mutual Exclusive
Mutual Exclusive helps keep threads from interfering with one another while sharing data. This
can be done in three ways in Java:
1. by
2. By synchronized block
3. By static synchronization
In this example, there is no synchronization, so the output is inconsistent. Let's see the
example:
class Table{
void printTable(int n){//method not synchronized
for(inti=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
}
}
}
}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}
class TestSynchronization1{
public static void main(String args[]){
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
}
}
Output:
5
100
10
200
15
300
20
400
25
500
}
}
}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}
Output:
5
10
15
20
25
100
200
300
400
500
Synchronized block in Java:
Synchronized block can be used to perform synchronization on any specific resource of the
method.
Suppose you have 50 lines of code in your method, but you want to synchronize only 5 lines,
you can use a synchronized block.
If you put all the codes of the method in the synchronized block, it will work the same as the
synchronized method.
class Table{
void printTable(int n){
synchronized(this){//synchronized block
for(inti=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
}
}
}//end of the method
}
class MyThread1 extends Thread{
Table t;
MyThread1(Table t){
this.t=t;
}
public void run(){
t.printTable(5);
}
}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}
public class TestSynchronizedBlock1
{
public static void main(String args[])
{
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
}
}
Output:
5
10
15
20
25
100
200
300
400
500
Output:
Deadlock in Java:
For example, consider the classic queuing problem, where one thread is producing some data
and another is consuming it. To make the problem more interesting, suppose that the producer
has to wait until the consumer is finished before it generates more data. In a polling system, the
consumer would waste many CPU cycles while it waits for the producer to produce. Once the
producer was finished, it would start polling, wasting more CPU cycles waiting for the consumer
to finish, and so on. Clearly, this situation is undesirable.
To avoid polling, Java includes an elegant interprocess communication mechanism via the wait(
), notify( ), and notifyAll( ) methods. These methods are implemented as final methods in Object,
so all classes have them. All three methods can be called only from within a synchronized
context.
wait()
notify()
notifyAll()
wait() method
Causes the current thread to wait until another thread invokes the notify().
Method
Description
public final void wait()throws InterruptedException
waits until the object is notified.
wait()
sleep()
wait() method releases the lock
sleep() method doesn't release the lock.
is the method of Object class
is the method of the Thread class
is the non-static method
is the static method
is the non-static method
is the static method
should be notified by notify() or notifyAll() methods
After the specified amount of time, sleep is completed.
Example :
class Customer{
int amount=10000;
synchronized void withdraw(int amount){
System.out.println("going to withdraw...");
if(this.amount<amount){
System.out.println("Less balance; waiting for deposit...");
try{wait();}catch(Exception e){}
}
this.amount-=amount;
System.out.println("withdraw completed...");
}
synchronized void deposit(int amount){
System.out.println("going to deposit...");
this.amount+=amount;
System.out.println("deposit completed... ");
notify();
}
}
class threadclass{
public static void main(String args[]){
final Customer c=new Customer();
new Thread(){
public void run(){c.withdraw(15000);}
}.start();
new Thread(){
public void run(){c.deposit(10000);}
}.start();
}}
output:
going to withdraw...
Less balance; waiting for a deposit...
going to deposit...
deposit completed...
withdraw completed..
Example2:
class Chat {
boolean flag = false;
public synchronized void Question(String msg) {
if (flag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(msg);
flag = true;
notify();
}
public synchronized void Answer(String msg) {
if (!flag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(msg);
flag = false;
notify();
}
}
Example3:
import java.io.*;
class Chat {
boolean flag = false;
output:
hi
Question:hi
hello
Answer:hello