Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

throw & throws keyword

In Java we have two keyword throw & throws keywords which are associated with Exception.

Use of throw keyword:

throw keyword is used to throw the exception from the body of the method. If you are throwing any unchecked exception then there is no hard and fast rule that you will have to report about that exception using try and catch or by propagating but when it throw a Checked Exception then we must have to handle it using try-catch block or we can use throws keyword to transfer the responsibility to handle that exception to the method caller.

            
class JTC{
	public static void main(String arg[]){
		try{
			new A().m1(Integer.parseInt(arg[0]));
		}catch(ArithmeticException e){
			System.out.println("ArithmeticException occured...!");
			System.out.println("you have passed 0 as argument...!");
		}catch(NumberFormatException e){
			System.out.println("NumberFormatException occured...!");
			System.out.println("you have to passed a -ve argument");
		}
	}
}
class A{
	void m1(int a){
		if(a > 0){
			System.out.println("The value of a is +ve so all set...!");
		}else if(a == 0){
			throw new ArithmeticException();
		}else{
			throw new NumberFormatException();
		}
	}
}
    C:\Users\Rahul\Desktop>java JTC 10
    The value of a is +ve so all set...!

    C:\Users\Rahul\Desktop>java JTC -10
    NumberFormatException occured...!
    you have to passed a -ve argument

    C:\Users\Rahul\Desktop>java JTC 0
    ArithmeticException occured...!
    you have passed 0 as argument...!
            

In the above example we have a method m1(int a), which is containing int type of parameter and on the basis of argument we are throwing exception from the m1 body such as if the argument is equal to 0 then it throw ArithmeticException and in the case of –ve argument throwing NumberFormatException.
And in the main method of class JTC we are handling both exceptions using try-catch block.

Use of throws keyword:

throws is used to throw the exception from the header of the method. Whenever you are throwing any exception by writing throws keyword, it means you are directing caller of the method to report about the exception in its own way (i.e., either by using try and catch or by propagating). If you are throwing any checked exception then the caller of the method must have to report about the exception by writing try and catch or by propagating. If you are not doing that then it will give compilation error. If you are throwing any unchecked exception by writing throws keyword then you may or may not handle that. If you are not handling it, it will automatically be handled by the JVM.

            
class JTC{
	public static void main(String arg[]){
		
			new A().m1(Integer.parseInt(arg[0]));
	}
}
class A{
	void m1(int a) throws ClassNotFoundException, java.io.FileNotFoundException{
		if(a > 0){
			System.out.println("The value of a is +ve so all set...!");
		}else if(a == 0){
			throw new ClassNotFoundException();
		}else{
			throw new java.io.FileNotFoundException();
		}
	}
}
    test.java:4: error: unreported exception ClassNotFoundException; must be caught or declared to be thrown
                new A().m1(Integer.parseInt(arg[0]));
                          ^
            

In this example we have throws java.lang.ClassNotFoundException and java.io.FileNotFoundException which are checked exception but we are not handling it at the time of m1 method invocation in JTC class main method and we are getting error at Compile-Time as we can see on the console so we have to handle it using try-catch block or if you do not want to handle it then we can use throws keyword in main method header to transfer the responsibilities to the JVM, because JMV invokes the main method internally.

            
class JTC{
	public static void main(String arg[]) throws ClassNotFoundException, java.io.FileNotFoundException{
		
			new A().m1(Integer.parseInt(arg[0]));
	}
}
class A{
	void m1(int a) throws ClassNotFoundException, java.io.FileNotFoundException{
		if(a > 0){
			System.out.println("The value of a is +ve so all set...!");
		}else if(a == 0){
			throw new ClassNotFoundException();
		}else{
			throw new java.io.FileNotFoundException();
		}
	}
}
    C:\Users\Rahul\Desktop>java JTC 10
    The value of a is +ve so all set...!

    C:\Users\Rahul\Desktop>java JTC -10
    Exception in thread "main" java.io.FileNotFoundException
        at A.m1(test.java:14)
        at JTC.main(test.java:4)

    C:\Users\Rahul\Desktop>java JTC 0
    Exception in thread "main" java.lang.ClassNotFoundException
        at A.m1(test.java:12)
        at JTC.main(test.java:4)
            

In this example we are not handling the ClassNotFoundException and java.io.FileNotFoundException using try-catch block in main method, but we are throwing the responsibility to handle both of the exception to the JVM by using throws keyword in main method header and as you can see JVM is generating the default exception reports as we can see on console.

            
class JTC{
	public static void main(String arg[]){
			try{
				new A().m1(Integer.parseInt(arg[0]));
			}catch(ClassNotFoundException e){
				System.out.println("Handling ClassNotFoundException using try-catch block explicitlly...!");
			}catch(java.io.FileNotFoundException e){
				System.out.println("Handling FileNotFoundException using try-catch block explicitlly....!");
			}
	}
}
class A{
	void m1(int a) throws ClassNotFoundException, java.io.FileNotFoundException{
		if(a > 0){
			System.out.println("The value of a is +ve so all set...!");
		}else if(a == 0){
			throw new ClassNotFoundException();
		}else{
			throw new java.io.FileNotFoundException();
		}
	}
}
    C:\Users\Rahul\Desktop>java JTC 10
    The value of a is +ve so all set...!

    C:\Users\Rahul\Desktop>java JTC -10
    Handling FileNotFoundException using try-catch block explicitlly....!

    C:\Users\Rahul\Desktop>java JTC 0
    Handling ClassNotFoundException using try-catch block explicitlly...!
            

In this example we are throwing the same Checked Exception same as above two given example but in this attempt we are handling both exception manually using try-catch block and providing custom report.

            
class JTC{
	public static void main(String arg[]){
			new A().m1(Integer.parseInt(arg[0]));
	}
}
class A{
	void m1(int a){
		if(a > 0){
			System.out.println("The value of a is +ve so all set...!");
		}else if(a == 0){
			throw new ArithmeticException();
		}else{
			throw new NumberFormatException();
		}
	}
}
    C:\Users\Rahul\Desktop>java JTC 10
    The value of a is +ve so all set...!

    C:\Users\Rahul\Desktop>java JTC -10 
    Exception in thread "main" java.lang.NumberFormatException
            at A.m1(test.java:13)
            at JTC.main(test.java:3)

    C:\Users\Rahul\Desktop>java JTC 0
    Exception in thread "main" java.lang.ArithmeticException
            at A.m1(test.java:11)
            at JTC.main(test.java:3)
            

In this example we have thrown ArithmeticException and NullPointerException which are Unchecked Exception, so as we discussed when we throw Unchecked Exceptions so there in no mandatory rules to handle exceptions and same concept we have implemented in this program as we can see we are not handling using try-catch block or not throwing to the JVM using throws keyword from main method header but we are not getting error at compile time and we are getting default exception report from JVM side as well.