JAVA UNIT-3 Lecture Notes
JAVA UNIT-3 Lecture Notes
Error Exception
Uncaught exceptions:
class Test
{ public static void main(String args[])
{
System.out.println("Hello");
System.out.println("VITS");
System.out.println(10/0);
System.out.println("Have a nice day");
System.out.println("Bye");
}
}
In the above code, the third line in the main() causes an exception, which is not
handled. JRE will use its default exception handling mechanism and prints the
exception information.
“finally” block
This block get executed after the completion of try block.
This block gets executed irrespective of whether the exception is raised or
not, if raised whether it is handled or not.
Useful for closing file handles and freeing up of any other resources that
might have been allocated at the beginning.
Every try block must be followed by at least one catch block or finally
block.
Example program:
class Test
{
public static void main(String[] args)
{
int a=10,b=0,c;
try
{
c=a/b;
System.out.println("result= "+c);
}
catch(ArithmeticException ae)
{
System.out.println(ae);
}
finally
{
System.out.println("This will execute");
}
System.out.println("END");
}
}
Multiple - catch blocks
class Test
{
public static void main(String[] args)
{
int a[] = {10,20,30,40,50,60};
try
{
// int c=a[10]/0;
int c=a[1]/0;
System.out.println("result= "+c);
}
catch(ArithmeticException ae)
{
System.out.println(ae);
}
catch(ArrayIndexOutOfBoundsException ab)
{
System.out.println(ab);
}
}
}
“multi catch” – catch block
This feature added to java from jdk1.7 version.
Allows two or more exceptions to be caught by the same catch clause.
Used when two or more catch blocks has the same code.
Each multi catch parameter is implicitly final. So it can't be assigned a new
value.
Example program:
class Test
{
public static void main(String[] args)
{
int a[] = {10,20,30,40,50,60};
try
{
int c=a[10]/0;
// int c=a[1]/0;
System.out.println("result= "+c);
}
catch(ArithmeticException | ArrayIndexOutOfBoundsException ae)
{
System.out.println(ae);
}
}
}
“catch all” - catch block
The catch block that takes an object of type Exception class as a parameter
is called catch all catch block.
Since Exception class is the super class for both checked and unchecked
exceptions, this catch block can catch any type of exception.
This block must be specified after all its sub class catch blocks if present.
Example program:
class Test
{ public static void main(String[] args)
{
int a[] = {10,20,30,40,50,60};
try
{
int x = Integer.parseInt("5a");
int c = a[10]/0;
System.out.println("result= "+c);
}
catch(ArithmeticException | ArrayIndexOutOfBoundsException ae)
{
System.out.println(ae);
}
catch(Exception e)
{
System.out.println(e);
}
}}
“nested try” - statements
using a try block with in another try block.
If an exception occur in the inner try block, if the inner try block is not
handling the exception then the exception object is thrown to outer try block.
If the outer try block also do not have the matching catch block, then the
exception object will be thrown to its caller.
Exampele program:
import java.util.*;
class Test
{ public static void main(String args[])
{ Scanner s = new Scanner(System.in);
System.out.println("enter the number");
int a = s.nextInt();
try
{
int b = 10/a;
System.out.println(b);
try
{
if (a==1) a = a/(a-a);
else
{
int c[] = {1,2,3};
c[4]=4;
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Invalid index");
}
}
catch(ArithmeticException e)
{
System.out.println("Divide be zero Error");
}
}
}
throw
To Manually throw an exception object.
Syntax:
throw throwable_instance;
When you manually throw an exception object from the try block ,it checks
for the matching catch block among the catch blocks which follows the try
block.
If there is a match, the exception is caught by that catch block and it can
process that exception(optional).
After processing, if needed the method can rethrow the exception object to
its caller to give an oppurtunity to the caller to know about this exception
and to process.
Example program:
class Test
{
static void meth1()
{
try
{
int x = 12/0;
}
catch(ArithmeticException e)
{
System.out.println("caught inside method:"+e);
throw e;
}
}
public static void main(String args[])
{ try
{
meth1();
}
catch(ArithmeticException e)
{
System.out.println("Recaught:"+e);
}
}}
throws
If a method is capable of causing an exception that it does not handle, it
must specify this behavior so that callers of this method can guard
themselves against that exception.
Mandatory for exceptions that are direct subclasses of Exception class.
Not necessary for exceptions that are subclasses of RuntimeException class
Example program:
import java.io.*;
class Test
{
static void meth1() throws FileNotFoundException
{
FileInputStream fis = null;
fis = new FileInputStream("ex1.dat");
}
public static void main(String args[])
{
try
{
meth1();
}
catch(FileNotFoundException e)
{
System.out.println("plz check your filename");
}
}}
“final rethrow” (added in jdk 1.7)
import java.io.*;
class Test
{ static void meth1(int x) throws IOException, InterruptedException
{
try
{
if( x == 0 )
throw new IOException();
else
throw new InterruptedException();
}
catch(Exception e)
{
throw e;
}
}
public static void main(String args[])
{ try
{
meth1(0);
}
catch(IOException | InterruptedException e)
{ System.out.println(e); }
}}
The Java SE 7 compiler can determine that the exception thrown by the
statement throw e must have come from the try block, and the only
exceptions thrown by the try block can be IOException or
InterruptedException.
Even though the exception parameter of the catch clause, e, is type
Exception, the compiler can determine that it is an instance of either
IOException or InterruptedException:
New: When a thread object is created using new, then the thread is said to be in the
New state. This state is also known as Born state.
Runnable / Ready: When a thread calls start( ) method, then the thread is said to
be in the Runnable state. This state is also known as a Ready state.
Running: When a thread calls run( ) method, then the thread is said to be Running.
The run( ) method of a thread called automatically by the start( ) method.
Blocked / Waiting
A thread in the Running state may move into the blocked state due to various
reasons like sleep( ) method called, wait( ) method called, suspend( ) method
called, and join( ) method called, etc.
When a thread is in the blocked or waiting state, it may move to Runnable state
due to reasons like sleep time completed, waiting time completed, notify( ) or
notifyAll( ) method called, resume( ) method called, etc.
Dead / Terminated
A thread in the Running state may move into the dead state due to either its
execution completed or the stop( ) method called. The dead state is also known as
the terminated state.
Creating Threads
Threads can be created in java in 2 ways
1. By extending Thread class.
2. By implementing Runnable interface.
sleep() -- method
Makes the current thread to suspend itself for a specified amount of time.
Time in milliseconds should be passes as a parameter to sleep method.
This method will throw InterruptedException if it fails to sleep the thread for the
specified amount of time.
Example
class MyThread implements Runnable
{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println("Child Thread:"+i);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
}
class Test
{
public static void main(String args[])
{
MyThread m = new MyThread();
Thread t1 = new Thread(m);
t1.start();
for(int i=0;i<10;i++)
{
System.out.println("Main Thread:"+i);
try
{
Thread.sleep(500);
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
}
Thread priorities:
An integer value that specify relative priority of one thread to another.
Among the threads of equal priority, JRE (Thread shedular) may schedule
threads in any order for execution.
Methods:
void setPriority(int priority)
int getPriority()
Priority value ranges from 0(low) to 10(high).
Normal priority is 5.
These are represeted by final static variables in Thread class
Thread.MIN_PRIORITY(0),
Thread.MAX_PRIORITY(10),
Thread.NORM_PRIORITY(5)
Thread schedular may give preference to high priority threads while
scheduling threads for execution.
Thread priorities are only to influence the thread schedular. Can't rely on
them.
Example:
class Test extends Thread
{
public void run()
{
for(int i=1;i<=10000;i++)
System.out.print("\nChild thread : "+i);
}
public static void main(String[] args)
{
Test t1 = new Test();
t1.setPriority(10);
t1.start();
Thread.currentThread().setPriority(1);
for(int i=1;i<=10000;i++)
System.out.print("\nMain thread :"+i);
}
}
join() - method
makes the the caller thread to wait until the thread on which join() has invoked
completes it execution.
Example:
class Test extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
System.out.print("\nChild thread : "+i);
}
public static void main(String[] args) throws InterruptedException
{
Test t1 = new Test();
t1.start();
t1.join();
for(int i=1;i<=5;i++)
System.out.print("\nMai Thread : "+i);
}
}
Synchronization
When two or more threads needs access to a shared resource, they need
some way to ensure that the resource will be used by only one thread at a
time. The process by which this is achieved is called synchronization.
Key to synchronization is the concept of monitor. A monitor is an object that
is used as a mutually exclusive lock.
Only one thread can own an object's 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.
All object have implicit monitor.
We can synchronize a shared resource in two ways
1. Synchronized methods:
Whichever the methods of the resource(object) you want to
synchronize, decalare those methods with synchronized modifier.
2. Synchronized block:
i) If you want to synchronize access to objects of a class that was not
designed for multithreaded access (that is the class does not use
snchronzed methods), we can use synchronized blocks.
ii) If the class was created by a third party, we do not have access to
the code. Then we can acquire lock on the object with synchronized
block.
Syntax:
synchronized(target_instance)
{
target_instance.method1();
}
Method Synchronization
class Display
{
public synchronized void wish(String name)
{
for(int i=0;i<10;i++)
{
System.out.print("Good morning : ");
try
{
Thread.sleep(2000);
}
catch(InterruptedException e) { }
System.out.println(name);
}
}
}
class Mythread extends Thread
{
Display d;
String name;
Mythread(Display d , String name)
{
this.d = d; this.name = name;
}
public void run()
{
d.wish(name);
}
}
class Test
{
public static void main(String args[])
{
Display d = new Display();
Mythread t1 = new Mythread(d,"SHIVA");
Mythread t2 = new Mythread(d,"RAVI KRIAN");
t1.start();
t2.start();
}
}
Example
class Buffer
{
int a ; boolean produced = false ;
public synchronized void produce(int x)
{
if(produced)
{
try{
wait();
}catch(Exception e){ System.out.println(e); }
}
a = x;
System.out.println("Product" + a + " is produced." );
produced = true;
notify();
}
public synchronized void consume()
{
if( !produced )
{
System.out.println("Consumer is waiting...");
try {
wait();
}
catch(Exception e)
{
System.out.println(e);
}
}
System.out.println("Product" + a + " is consumed.");
produced = false;
notify();
}
}
class Producer extends Thread
{
Buffer b;
Producer(Buffer b)
{
this.b = b;
}
public void run()
{
System.out.println("Producer start producing...");
for(int i = 1; i <= 10; i++)
{
b.produce(i);
}
}
}