Home ProgrammingJava Exception Handling in Java: Detailed guide on throw and throws; which to use when? 4 differences with examples

Exception Handling in Java: Detailed guide on throw and throws; which to use when? 4 differences with examples

by Log Raj Bhatt
18 views
Code Example & Difference between Throw and Throws

Dear learners, after long time I am back with very interesting and buzzed topic among java learners; throw and throws. These both keywords are little confusing among newbies and difference between throw and throwsis frequently asked in different examinations and interviews.

As a developer, it is also very important to understand the concept behind these both.

Introduction to throw and throws:

I think the difference among these two keywords is hidden inside its spelling.  Throw is to throw an exception during the execution of program while throws is declaration statement the following code can throw the exception. First is to raise exception and second is to let the compiler know that the code might throw exception in certain cases.

Exception Handling in Java: Throw and Throws

Differences between throw and throws:

I have pointed the differences in table below. Please go through every line and try to understand code below

throwthrows
throw keyword in java explicitly throw an exception from any part of java code.throws keyword is to tell the compiler that the following code can throw some kind of exception. So It is part of method signature.
This will throw an instance of any Exception Class. It will produce only the object of throwable object.Throws keyword declares the name of throwable class which can occur inside the block of code.
throw keyword can only one throwable exception at a time.throws keyword can declare multiple exceptions separated by commas.
Checked exception are not propagated using throw only but unchecked exception are propagated using throw.This allow checked exception to propagate.

Example Code using throw and throws

Example illustrating throw keyword.

package packOne;
public class MainWala {
	public static void main(String []args){
		System.out.println("starting codes ");
		try{	
			System.out.println("risky code ");
			throw new NullPointerException();	
		}
		catch(NullPointerException e){	
			System.out.println("NPE"+e.getMessage());	
		}
		catch(Exception e){	
			System.out.println("E"+e.getMessage());
			
		}finally{
			System.out.println("Finally i'm important");
		}	
		System.out.println("Other code");
	}
}

Output:

starting codes 
risky code 
NPEnull
Finally i'm important
Other code

Example illustrating throws keyword.

package mainwala;
import java.io.IOException;

public class MainWala {
	public static void test() throws ArithmeticException{
		System.out.println(“test method”);
		}
			public static void test1() throws IOException{
			System.out.println(“test 1 method”);
			}
			public static void main(String[] args) {
			MainWala.test();
			try{
			MainWala.test1(); // this code should be checked
			}
			catch (Exception e){
			System.out.println(“catched”);
			}
		}
	}

Output:

run:
test method
test 1 method

Code Example & Difference between Throw and Throws

Conclusion:

throw is to generate an exception object at the middle of code. That object will be handled in some way. But throws is to let compiler know this method will throw an exception. So, while calling a method with throws clause compiler is already alert if there is checked exception. And throw is followed by an object of exception class and throws keyword is followed by a single Exception class or set of Exception classes separated by comma.

Happy learning to all readers and best of luck. You can ask the queries if you any and suggest about the content. Subscribe to our YouTube channel to known about latest programming tutorials.

Leave a Reply

Do NOT follow this link or you will be banned from the site!