fastread homefastrread library fastread menu

Java : Difference between throw and throws

Tutorial by:Arena Buocel      Date: 2016-04-30 01:52:07

❰ Previous Next ❱

There are many differences between throw and throws keywords. A list of differences between throw and throws are given below:

No. throw throws
1) Java throw keyword is used to explicitly throw an exception. Java throws keyword is used to declare an exception.
2) Checked exception cannot be propagated using throw only. Checked exception can be propagated with throws.
3) Throw is followed by an instance. Throws is followed by class.
4) Throw is used within the method. Throws is used with the method signature.
5) You cannot throw multiple exceptions. You can declare multiple exceptions e.g.
public void method()throws IOException,SQLException.

Java throw example

 
  1. void m(){  
  2. throw new ArithmeticException("sorry");  
  3. }  

Java throws example

 
  1. void m()throws ArithmeticException{  
  2. //method code  
  3. }  

Java throw and throws example

 
  1. void m()throws ArithmeticException{  
  2. throw new ArithmeticException("sorry");  
  3. }  

❰ Previous Next ❱


Java

Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website