Monday, 29 January 2018

7.0 MultiThreading in Java

Multitasking - executing multiple tasks simultaneously.

  • process based multitasking
Executing multiple task simultaneously where each task is separate process
  • Thread based multitasking
Executing multiple task simultaneously where each task is independent but part of same program.

main advantage is to increase performance and to reduce response time of system.


MULTITHREADING - Multiple threads executing simultaneously

Thread : A light weight process.
A separate flow of execution.

Defining Thread -  
  1. By extending Thread class
  2. By implementing Runnable interface



class ThreadDemo
{
public static void main(String args[])
{
MyThread obj=new MyThread();
obj.start();
for(int i=1;i<=10;i++)
{
System.out.println("Main Thread : " +i);
}
}
}
class MyThread extends Thread
{
public void run()
{
for(int i=1;i<=10;i++)
{
System.out.println("Child Thread : " +i);
}
}
}


C:\Users\Aarchi\Desktop\Java Development>java ThreadDemo
Main Thread : 1
Main Thread : 2
Main Thread : 3
Child Thread : 1
Main Thread : 4
Main Thread : 5
Child Thread : 2
Main Thread : 6
Child Thread : 3
Main Thread : 7
Child Thread : 4
Main Thread : 8
Child Thread : 5
Main Thread : 9
Child Thread : 6
Main Thread : 10
Child Thread : 7
Child Thread : 8
Child Thread : 9
Child Thread : 10




Thread scheduler - It is a part of JVM and responsible for threads execution.
In which order threads will execute is decide by thread scheduler. 

Overloading of run() - is always possible but thread class start() can invoke no-arg run ().
The other overloaded method can be invoked explicitly like a normal method call.

If you'll not override run() in you class then Thread class run() will get executed which has empty implementation hence we'll not get any output.

If we override start() then our start() will be executed just like a normal method call and new thread won't be created.

THREAD LIFE CYCLE









After starting a thread if we are trying to restart the same thread then we'll get run time exception.
saying IllegalThreadStateException.

Thread t=new Thread();
t.start();
t.start();


Defining a thread by implementing Runnable Interface.

Runnable interface present in java.lang package and only contain one method run().



import java.lang.*;
class ThreadDemo1
{
public static void main(String args[])
{
MyRunnable obj=new MyRunnable();
Thread t=new Thread(obj);
t.start();
for(int i=1;i<=10;i++)
{
System.out.println("Main Thread : "+i);
}
}
}

class MyRunnable implements Runnable
{
public  void run()
{
for(int i=1;i<=10;i++)
{
System.out.println("Child Thread : "+i);
}
}
}

C:\Users\Aarchi\Desktop\Java Development>java ThreadDemo1
Main Thread : 1
Main Thread : 2
Main Thread : 3
Main Thread : 4
Main Thread : 5
Child Thread : 1
Child Thread : 2
Main Thread : 6
Child Thread : 3
Main Thread : 7
Child Thread : 4
Main Thread : 8
Child Thread : 5
Main Thread : 9
Main Thread : 10
Child Thread : 6
Child Thread : 7
Child Thread : 8
Child Thread : 9
Child Thread : 10


CASE STUDY

NOTE : implementation of Runnable interface is recommended
If we'll extend Thread class then we can't extend any other class.


THREAD CLASS CONSTRUCTORS
  1. Thread t = new Thread();
  2. Thread t = new Thread(Runnable r);
  3. Thread t = new Thread(String name);
  4. Thread t = new Thread(Runnable r, String name);
  5. Thread t = new Thread(ThreadGroup g, String name);
  6. Thread t = new Thread(ThreadGroup g, Runnable r);
  7. Thread t = new Thread(ThreadGroup g, Runnable r, String name);
  8. Thread t = new Thread(ThreadGroup g, Runnable r, String name, Long stacksize);
class MyThread extends Thread

MyThread obj=newMyThread();
Thread t=new Thread(obj);
t.start();

Thi is valid
MyThread ------>Thread------->Runnable

  1. Thread t = new Thread(Runnable r);
we can pass MyThread object here.



To get and set Thread names

class ThreadDemo2
{
public static void main(String args[])
{
System.out.println(Thread.currentThread().getName());
MyThreada obj=new MyThreada();
System.out.println(obj.getName());
Thread.currentThread().setName("Bikesh");
System.out.println(Thread.currentThread().getName());
System.out.println(10/0);
}
}
class MyThreada extends Thread
{
}


C:\Users\Aarchi\Desktop\Java Development>java ThreadDemo2
main
Thread-0
Bikesh
Exception in thread "Bikesh" java.lang.ArithmeticException: / by zero
        at ThreadDemo2.main(ThreadDemo2.java:10)


Thread Priorities
Every thread in java have some priority.
Range of Priorities - 1(min) to 10(max)
Thread.MIN_PRIORITY=1
Thread.NORM_PRIORITY=5
Thread.MAX_PRIORITY=10
Thread.LOW_PRIORITY - Invalid

Thread scheduler will set the priority while allocating processor.
Thread which is having highest priority will get the chance first.
If two threads have same priority then we can't expect exact execution order. It depends on Thread scheduler.

Thread class defines the following methods to get and set priority of the thread.
public final int getPriority();
public final void setPriority(int p);
where p ranges from 1 to 10.
if it is not then we'll get IllegalArgumentException.


default priority for every thread is 5. (InCorrect) default priority of all thread inherited from parent to child. i.e whatever priority parent thread has , the same priority will be there for the child thread.
OR
default priority for only main thread is 5. (Correct)


class ThreadDemo2
{
public static void main(String args[])
{
System.out.println("Priority of "+Thread.currentThread().getName() +" = "+Thread.currentThread().getPriority());
Thread.currentThread().setPriority(8);
MyThreada obj=new MyThreada();
System.out.println("Priority of "+Thread.currentThread().getName() +" = "+Thread.currentThread().getPriority());
System.out.println("Priority of "+obj.getName() +" = "+obj.getPriority());
}
}
class MyThreada extends Thread
{
public void run(){}
}


C:\Users\Aarchi\Desktop\Java Development>javac ThreadDemo2.java

C:\Users\Aarchi\Desktop\Java Development>java ThreadDemo2
Priority of main = 5
Priority of main = 8
Priority of Thread-0 = 8

Thread.currentThread().setPriority(18);

Exception in thread "main" java.lang.IllegalArgumentException
        at java.lang.Thread.setPriority(Thread.java:1089)
        at ThreadDemo2.main(ThreadDemo2.java:10)


Some platforms won't provide support for thread priorities.
In Spite of setting highest priority to a thread , you may not get the desired output.


PREVENT THREAD EXECUTION
  1. yield()
  2. join()
  3. sleep()
yield() - causes to pass current executing thread to give the chance for waiting thread of same priority.
if there is no waiting thread or all waiting threads have low priority then same thread can continue its execution.
The thread which is yielded, when it will get the chance once again depends upon thread scheduler.
We can't expect exact immediate regaining the execution engine.

join() -
If a thread wants to wait until completing some other thread then we should go for join().
for ex. if a thread t1 wants to wait until completing t2 then t1 has to call t2.join()
If t1 executes t2.join() then  immediately t1 will be entered into waiting state until t2 completes.
Once t2 completes then t1 can continue its execution.

public void final join() throws InterruptedException;
public void final join(long ms) throws InterruptedException;

public void final join(long ms , in ns) throws InterruptedException;

while a thread waiting until completing some other thread then there may be a chance interrupted exception. Therefore every join method throws InterruptedException which is checked exception exception compulsory we should handle this exception either by using try catch or by throws keyword.

Example to get main thread to apply join

class MyThreadDemo extends Thread
{
static Thread mt;
public void run()
{
try{
mt.join();
}
catch(InterruptedException ie)
{
System.out.println(ie.getMessage());
}
for(int i=1;i<=10;i++)
{
System.out.println("Child Thread : "+i);
}
}
}

class ThreadDemo3
{
public static void main(String args[])
{
MyThreadDemo.mt=Thread.currentThread();
MyThreadDemo obj=new MyThreadDemo();
obj.start();
for(int i=1;i<=10;i++)
{
System.out.println("Main Thread : "+i);
}
}
}

If main thread also put obj.join() then a deadlock situation will arise.
If main thread put join on main thread itself then also deadlock situation will arise.
Thread.currentThread().join();

sleep() - If a thread don't want to perform any operation for a particular amount of time then we should go for sleep().
public static native void sleep(long ms) throws InterruptedException;
public static void sleep(long ms, int ns) throws InterruptedException;
Every sleep method throws IE Exception, which is checked exception hence whenever we are using sleep method , compulsory we should handle InterruptedException either by try catch or by throws keyword.

class ThreadDemo4
{
public static void main(String args[]) throws InterruptedException
{
for(int i=1;i<=10;i++)
{
System.out.println("Slider = "+i);
Thread.sleep(5000);
}
}
}


interrupt() - example of how a thread can interrupt another thread

class ThreadDemoInterrupt extends Thread
{
public void run()
{
try{
for(int i=1;i<=10;i++)
{
System.out.println("Lazy Thread = "+i);
Thread.sleep(2000);
}
}
catch(InterruptedException ie)
{
System.out.println("I got interrupted");
}
}
}

class ThreadDemo4
{
public static void main(String args[])
{
ThreadDemoInterrupt obj=new ThreadDemoInterrupt();
obj.start();
obj.interrupt();
System.out.println("End of main THread");
}
}

Note : Whenever we are calling interrupt() if the target thread not in sleeping state or waiting state then there is no impact of interrupt call immediately. Interrupt call will wait until target thread enter into sleeping or waiting state. If the target thread enter into sleeping or waiting state , immediately interrupt call will interrupt the target thread. 
If the target thread never entered into sleep or waiting state in its lifetime then there is no impact of interrupt call. This is the only case where interrupt call will be wasted.
























Synchronization - It is a modifier .It is only applicable for methods and block but not for classes and variables

If multiple threads try to operate simultaneously on a single java object then this type of problem is called object inconsistency problem.

To resolve data inconsistency we can use synchronized keyword.

if a method or block declared as synchronized then at a time only one thread is allowed to execute that method or block on the given object.

The main advantage of synchronized keyword is , we can resolve data inconsistency problems but the main disadvantage is , it increases waiting time of threads and creates performance problems. Hence if there is no specific requirement then it is not recommended to use synchronized keyword.

Internally synchronization concept is implemented by using lock.
Every object in java has a unique lock. Whenever we are using synchronized keyword then only lock concept will come into picture.
If a thread wants to execute a synchronized method on the given object , first it has to get lock of that object.
Once thread got the lock then it is allowed to execute any synchronized method on that object. 
Once method execution completes , automatically thread releases lock. So acquiring and releasing lock internally takes care by JVM.



















t1 got the lock and start execution of m1()
while t1 was executing t2 came but it will go into waiting state.
t3 came to access m2 method. But to execute m2 object lock is required which is currently with t1 therefore t3 will also go into waiting state.
t4 came to access m3, it will directly able to execute because no lock is required to execute a non-synchronized method.


NON-SYNCHRONIZED EXAMPLE

class NonSync
{
public static void main(String args[])
{
Display obj=new Display();
MyThr t1=new MyThr(obj,"Bikesh");
MyThr t2=new MyThr(obj,"Amitesh");
MyThr t3=new MyThr(obj,"Maurya");
t1.start();
t2.start();
t3.start();
}
}

class Display
{
public void wish(String name)
{
for(int i=1;i<=10;i++)
{
System.out.println("Good Morning : " + name);
try{
Thread.sleep(1000);
}
catch(InterruptedException ie)
{}
}
}
}

class MyThr extends Thread
{
Display d;
String name;
public MyThr(Display d,String name)
{
this.d=d;
this.name=name;
}
public void run()
{
d.wish(name);
}
}

C:\Users\Aarchi\Desktop\Java Development>javac NonSync.java

C:\Users\Aarchi\Desktop\Java Development>java NonSync
Good Morning : Amitesh
Good Morning : Maurya
Good Morning : Bikesh
Good Morning : Amitesh
Good Morning : Bikesh
Good Morning : Maurya
Good Morning : Amitesh
Good Morning : Maurya
Good Morning : Bikesh
Good Morning : Amitesh
Good Morning : Maurya
Good Morning : Bikesh
Good Morning : Maurya
Good Morning : Bikesh
Good Morning : Amitesh
Good Morning : Maurya
Good Morning : Bikesh
Good Morning : Amitesh
Good Morning : Maurya
Good Morning : Amitesh
Good Morning : Bikesh
Good Morning : Maurya
Good Morning : Amitesh
Good Morning : Bikesh
Good Morning : Maurya
Good Morning : Amitesh
Good Morning : Bikesh
Good Morning : Maurya
Good Morning : Amitesh
Good Morning : Bikesh


SYNCHRONIZED EXAMPLE

just add the synchronized modifier in the method
public synchronized void wish(String name)

C:\Users\Aarchi\Desktop\Java Development>javac NonSync.java

C:\Users\Aarchi\Desktop\Java Development>java NonSync
Good Morning : Maurya
Good Morning : Maurya
Good Morning : Maurya
Good Morning : Maurya
Good Morning : Maurya
Good Morning : Maurya
Good Morning : Maurya
Good Morning : Maurya
Good Morning : Maurya
Good Morning : Maurya
Good Morning : Bikesh
Good Morning : Bikesh
Good Morning : Bikesh
Good Morning : Bikesh
Good Morning : Bikesh
Good Morning : Bikesh
Good Morning : Bikesh
Good Morning : Bikesh
Good Morning : Bikesh
Good Morning : Bikesh
Good Morning : Amitesh
Good Morning : Amitesh
Good Morning : Amitesh
Good Morning : Amitesh
Good Morning : Amitesh
Good Morning : Amitesh
Good Morning : Amitesh
Good Morning : Amitesh
Good Morning : Amitesh
Good Morning : Amitesh

If multiple thread operating on same object then only synchronization required.
In below case we have created two different object and t1 and t2 will lock obj1 and obj2, therefore synchronization is not required here. In spite of having synchronize method we'll get irregular output.

class NonSync
{
public static void main(String args[])
{
Display obj1=new Display();
                Display obj2=new Display();
MyThr t1=new MyThr(obj1,"Bikesh");
MyThr t2=new MyThr(obj2,"Amitesh");
t1.start();
t2.start();
}
}

static synchronized method will resolve this issue
public static synchronized void wish(String name)

Class level lock - every class in java has a unique lock which is also known as class level lock. 

If a thread wants to execute any static synchronized method then it require class level lock is required instead of object lock required.
























class bank
{
public static void main(String args[])
{
locker l1=new locker();
locker l2=new locker();
MyThreadDemoBank1 t1=new MyThreadDemoBank1(l1,"Bikesh");
MyThreadDemoBank2 t2=new MyThreadDemoBank2(l2,"Amitesh");
t1.start();
t2.start();
}
}

class locker
{
static int amount=1000;
static int translimit=0;
public static synchronized void account(String name)
{
while (amount > 0)
{
translimit++;
int total=amount;
amount=amount-100;
try
{
Thread.sleep(1000);
}
catch (InterruptedException ie)
{
}
System.out.println("Transaction : "+total+" - 100 ( "+name+" withdraw) ="+amount);
if(translimit==5)
{translimit=0;break; }
}
}
}

class MyThreadDemoBank1 extends Thread
{
String name;
locker l;
public MyThreadDemoBank1(locker l,String name)
{
this.l=l;
this.name=name;
}
public void run()
{
l.account(name);
}
}

class MyThreadDemoBank2 extends Thread
{
String name;
locker l;
public MyThreadDemoBank2(locker l,String name)
{
this.l=l;
this.name=name;
}
public void run()
{
l.account(name);
}
}

C:\Users\Aarchi\Desktop\Java Development>javac bank.java

C:\Users\Aarchi\Desktop\Java Development>java bank
Transaction : 1000 - 100 ( Amitesh withdraw) =900
Transaction : 900 - 100 ( Amitesh withdraw) =800
Transaction : 800 - 100 ( Amitesh withdraw) =700
Transaction : 700 - 100 ( Amitesh withdraw) =600
Transaction : 600 - 100 ( Amitesh withdraw) =500
Transaction : 500 - 100 ( Bikesh withdraw) =400
Transaction : 400 - 100 ( Bikesh withdraw) =300
Transaction : 300 - 100 ( Bikesh withdraw) =200
Transaction : 200 - 100 ( Bikesh withdraw) =100
Transaction : 100 - 100 ( Bikesh withdraw) =0


SYNCHRONIZED BLOCK
If very few lines of the code required synchronization then it is not recommended to declare entire method as synchronized. We have to enclose those few lines of the code by using synchronized block. 

The main advantage of synchronized block over synchronized method is it reduces waiting time of threads and improves performance of the system/application.

synchronized(this)
{
..............
...............
..............
}
this is use to get the lock of current object

to get lock of particular project
synchronized(b)
{
.............
.............
.............
}
if a thread got the lock of object b then only it is allowed to execute this block.

to get class level lock
synchronized(Display.class)
{
...............
..............
..............
}
if a thread want to get the class level lock of class Display.

Lock concept only applicable for object types and class types but not for primitives.Hence we can't pass primitive type as argument to synchronized block otherwise we'll get compile time error saying "unexpected type fount int required ref."

int x=1;
ex: 
synchronize(x)
{
..............
...........
...............
}


QUICK REVISION:
  1. What is synchronized keyword and where we can apply ?
  2. Explain advantage of synchronized keyword.
  3. Explain disadvantage of synchronized keyword.
  4. What is race condition ? (data inconsistency problem)
  5. What is object lock and when it is required ?
  6. What is class level lock and when it is required ?
  7. Difference between class level and object level lock ?
  8. What is synchronized block ?
  9. How to declare synchronized block to get lock of current object, class level lock ?
  10. what is the advantage of synchronize block over synchronized method.
  11. is a thread can acquire multiple locks simultaneously ? (Yes)  



Read More »

Thursday, 25 January 2018

6.0 Exception Handling in java

Exception - It is an event - Abnormal termination of a program because of so and so reason.

Rest of the code will not run after unexpected event.

The main objective of exception handling is graceful termination of program.
Defining alternative way to complete the program normally is the concept of exception handling.
It doesn't mean repairing an exception.




To overcome the problem we use exception handling - To execute the full code without any unexpected termination


Runtime Stack mechanism
Jvm will create a runtime stack, each method will be get in the stack. Those entry are called activation record or stack frame. After completing every method call the corresponding entry from the stack will be removed .After completing all methods call the stack will become empty and that empty stack will be destroyed by JVM just before terminating the thread.

If something goes wrong

class TestRuntimeStackException                                                                         
{                                                                                                                             
public static void main(String args[])                                                            
{                                                                                                                     
TestRuntimeStackException obj=new TestRuntimeStackException();
obj.doStuff();                                                                                         
}                                                                                                                     
public void doStuff()                                                                                     
{                                                                                                                     
doMoreStuff();                                                                                       
}                                                                                                                     
public void doMoreStuff()                                                                             
{                                                                                                                     
System.out.println(10/0);                                                                       
System.out.println("Hello");                                                                  
}                                                                                                                     
}                                                                                                                             

C:\Users\Aarchi\Desktop\Java Development>javac TestRuntimeStackException.java

C:\Users\Aarchi\Desktop\Java Development>java TestRuntimeStackException
Exception in thread "main" java.lang.ArithmeticException: / by zero
        at TestRuntimeStackException.doMoreStuff(TestRuntimeStackException.java:14)
        at TestRuntimeStackException.doStuff(TestRuntimeStackException.java:10)
        at TestRuntimeStackException.main(TestRuntimeStackException.java:6)

Inside a method if an exception occured the method in which it occurs responsible to create exception object. This method hand over this object to the JVM. 
JVM will ask In which area it happens (doMoreStuff) - Is there exception handling  ? no there is no handling code - JVM will check who is the caller of doMoreStuff. JVM will delete that method from the stack and ask doStuff - is there any handling code - doStuff is not having any handling code. JVM will terminate this method immediately and delete from stack. This method is called by main. But main is also not handled this exception. JVM will asks same questions to main method. JVM will terminate the main method abnormally and will remove the corresponding entry from stack. It is not handled by anyone ,JVM will give this responsibility to default exception handler. Default exception handler will print the exception in the console.

Exception Handling Chart


There are two ways to handle exception



  1. Try catch block
  2. Throws keyword
Type of exception
  1. unchecked
  2. checked
  3. Error

UNCHECKED EXCEPTION



class exceptionpart1
{
public static void main(String args[])
{
System.out.println("Hello world");
System.out.println("Hello world");
System.out.println(10/0);
System.out.println("Hello world");
System.out.println("Hello world");

}
}











class class exceptionpart1
{
public static void main(String args[])
{
//System.out.println(10/0);
int[] a={10,20,30,40};
System.out.println(a[0]);
System.out.println(a[10]);
System.out.println("rest of the application");

}
}





  • Code is compiled but not running and giving ArithmeticException in example 1 and ArrayIndexOutOfBoundsException in example 2.
  • The exception which are unable to check by compiler those are known as unchecked exception


CHECKED EXCEPTIONS

class exceptionpart1

{
public static void main(String args[])
{
System.out.println("Hello world");
System.out.println("Hello world");
System.out.println("Hello world");
Thread.sleep(3000); //3sec
System.out.println("Hello world");
System.out.println("Hello world");

}

}

C:\Users\Aarchi\Desktop\Java Development>javac exceptionpart1.java

exceptionpart1.java:8: error: unreported exception InterruptedException; must be caught or declared to be thrown
                Thread.sleep(3000); //3sec
                            ^
1 error

Thread.sleep(3000) - thread is going into sleep and other threads can  interrupt which can cause abnormal termination of the code therefore compiler conveying information to developer that use try catch.


  • The exceptions which are checked by compiler are called checked exception.
  • The code will not compile
  • The exception which are checked by compiler for smooth execution of the program at run time is known as checked exception.
NOTE : Exception occurs only at run time whether it is checked or unchecked


TO PRINT EXCEPTION DETAILS


If try with multiple catch blocks are present then order of catch block is very important.
We have to take child first and then parent otherwise we will get compile error saying
exception xxx has already been caught.
Final Finally Finalize

IMPORTANT POINTS ABOUT TRY CATCH AND FINALLY

  1. In try catch finally order is important.
  2. try without catch or finally is invalid
  3. try catch or try finally is fine
  4. try finally catch is invalid
  5. try finally catch - curly braces is must.
  6. try catch in try or catch or finally block is valid.

THROW and THROWS

Sometimes we can create exception object explicitly and we can handover to the JVM manually, for this we have to use THROW keyword.

throw new ArithmeticException("/ by zero ");

Throw keyword is use to handover our created exception object to the jvm manually.

best use of throw is use to throw user defined exceptions.
Example 
widraw=20000
if (ballance < 10000)
throw new InsuffeciantFundException("Low balance");


CASE 1 :


class expcase1
{
static ArithmeticException e=new ArithmeticException();
public static void main(String args[])
{
throw e;
}
}

C:\Users\Aarchi\Desktop\Java Development>javac expcase1.java

C:\Users\Aarchi\Desktop\Java Development>java expcase1
Exception in thread "main" java.lang.ArithmeticException
        at expcase1.<clinit>(expcase1.java:3)


class expcase1
{
static ArithmeticException e;
public static void main(String args[])
{
throw e;
}
}

C:\Users\Aarchi\Desktop\Java Development>javac expcase1.java

C:\Users\Aarchi\Desktop\Java Development>java expcase1
Exception in thread "main" java.lang.NullPointerException
        at expcase1.main(expcase1.java:6)



CASE 2 :  you can't write any code immediately  after throw statement because compiler already now that exception throwing will terminate the program and the next line will be unreachable. You'll get compile time error.

class expcase1
{
public static void main(String args[])
{
System.out.println(10/0);
System.out.println("Hello World");
}

}


C:\Users\Aarchi\Desktop\Java Development>javac expcase1.java

C:\Users\Aarchi\Desktop\Java Development>java expcase1
Exception in thread "main" java.lang.NullPointerException
        at expcase1.main(expcase1.java:6)


class expcase1
{
public static void main(String args[])
{
throw new ArithmeticException("/ by zero");
System.out.println("Hello World");
}
}

C:\Users\Aarchi\Desktop\Java Development>javac expcase1.java
expcase1.java:6: error: unreachable statement
                System.out.println("Hello World");
                ^
1 error


CASE 3 : throw is only use for throwable type object not any other java object.
It will give you java.lang.throwable

class test
{
public static void main(String args[])
{
throw new test();
}
}

C:\Users\Aarchi\Desktop\Java Development>javac test.java
test.java:5: error: incompatible types: test cannot be converted to Throwable
                throw new test();
                ^
1 error

class test extends RuntimeException // now it is throwable
{
public static void main(String args[])
{
throw new test();
}
}

C:\Users\Aarchi\Desktop\Java Development>javac test.java

C:\Users\Aarchi\Desktop\Java Development>java test
Exception in thread "main" test
        at test.main(test.java:5)


THROWS - In our program if there is possibility of raising checked exception then compulsory we should handle that checked exception otherwise we'll get compile time error saying "unreported exception xxx must be caught or declared to be thrown.

import java.io.*;
class test
{
public static void main(String args[])
{
PrintWriter pw=new PrintWriter("abc.txt");
pw.write("Hello");
}
}

C:\Users\Aarchi\Desktop\Java Development>javac test.java
test.java:6: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
                PrintWriter pw=new PrintWriter("abc.txt");
                               ^
1 error


class test
{
public static void main(String args[])
{
Thread.sleep(10000);
}
}

C:\Users\Aarchi\Desktop\Java Development>javac test.java
test.java:5: error: unreported exception InterruptedException; must be caught or declared to be thrown
                Thread.sleep(10000);
                            ^
1 error

we can handle these compile time error  by using following two ways.

  1. By using try catch block
  2. By using throws keyword
we can use throws keyword to delegate responsibility of exception handling to the caller (It may be another method or JVM) then caller method is responsible to handle that exception.

public static void main(String args[]) throws InterruptedException
  • Throws keyword required only for checked exceptions .
  • Throws keyword only requires to convince the compiler and usage of throws keyword doesn't prevent abnormal termination of the program.
  • throws keyword we can only use with method,constructor not with class.
  • We can use throws keyword only for throwable types. If we will try to use it for normal java classes then we will get compile time error saying "incompatible types
DIFFERENT LEVEL OF DELEGATION


class test
{
public static void main(String args[]) throws InterruptedException
{
doStuff();
}
public static void doStuff()
{
doMoreStuff();
}
public static void doMoreStuff()
{
Thread.sleep(1000);
}

}


C:\Users\Aarchi\Desktop\Java Development>javac test.java
test.java:13: error: unreported exception InterruptedException; must be caught or declared to be thrown
                Thread.sleep(1000);
                            ^
1 error

class test
{
public static void main(String args[]) 
{
doStuff();
}
public static void doStuff() 
{
doMoreStuff();
}
public static void doMoreStuff() throws InterruptedException
{
Thread.sleep(1000);
}
}

C:\Users\Aarchi\Desktop\Java Development>javac test.java
test.java:9: error: unreported exception InterruptedException; must be caught or declared to be thrown
                doMoreStuff();
                           ^
1 error


class test
{
public static void main(String args[]) 
{
doStuff();
}
public static void doStuff() throws InterruptedException
{
doMoreStuff();
}
public static void doMoreStuff() throws InterruptedException
{
Thread.sleep(1000);
}
}

C:\Users\Aarchi\Desktop\Java Development>javac test.java
test.java:5: error: unreported exception InterruptedException; must be caught or declared to be thrown
                doStuff();
                       ^
1 error


class test
{
public static void main(String args[]) throws InterruptedException
{
doStuff();
}
public static void doStuff() throws InterruptedException
{
doMoreStuff();
}
public static void doMoreStuff() throws InterruptedException
{
Thread.sleep(1000);
}
}

C:\Users\Aarchi\Desktop\Java Development>javac test.java

C:\Users\Aarchi\Desktop\Java Development>java test

C:\Users\Aarchi\Desktop\Java Development>


DoMoreStuff is not delegating the exception to main below

class test
{
public static void main(String args[]) throws InterruptedException
{
doStuff();
}
public static void doStuff()
{
doMoreStuff();
}
public static void doMoreStuff() 
{
Thread.sleep(1000);
}

}

C:\Users\Aarchi\Desktop\Java Development>javac test.java
test.java:13: error: unreported exception InterruptedException; must be caught or declared to be thrown
                Thread.sleep(1000);
                            ^

1 error


THROWS - In our program if there is possibility of raising checked exception then compulsory we should handle that checked exception otherwise we'll get compile time error saying "unreported exception xxx must be caught or declared to be thrown.




FULLY CHECKED EXCEPTION - 






















List of compile time errors


  1. unreported exception InterruptedException; must be caught or declared to be thrown
  2. Exception xxx has already been caught
  3. Exception xxx is never thrown in boy of corresponding try statement.
  4. Unreachable statements
  5. incompatible types
  6. try without catch or finally
  7. catch without try
  8. finally without try.
Customized or User defined Exceptions
Sometimes to meet programming requirements we can define our own exceptions such types of exceptions are called Customized or user defined exceptions.

It is highly recommended to define our customized exceptions as unchecked i.e we have to extend runtimeException but not Exception.


class InvalidAgeException extends Exception{  
 InvalidAgeException(String s){  
  super(s);  
 }  
}  

class TestCustomException1{  
  
   static void validate(int age)throws InvalidAgeException{  
     if(age<18)  
      throw new InvalidAgeException("not valid");  
     else  
      System.out.println("welcome to vote");  
   }  
     
   public static void main(String args[]){  
      try{  
      validate(13);  
      }catch(Exception m){System.out.println("Exception occured: "+m);}  
  
      System.out.println("rest of the code...");  
  }  
}  
Read More »