throw and throws keyword in java

throw and throws keyword in java

Java throw keyword

in java throw keyword is used to throw an exception explicitly to the catch block. throw keyword can be used to throw a custom exception also.

Syntax for throw keyword

throw new ExceptionName("message");

If we throw an unchecked exception for example : NullPointerException or ArithmeticException there is no need to handle these type of exceptions because these will occur due to programming error, we can correct these errors

Example for the unchecked exception

class exec3{
static int sum(int num1, int num2){
if (num2 == 0)
throw new ArithmeticException("Second parameter is 0");
else
System.out.println("Both parameters are correct!!");
return num1+num2;
}
public static void main(String args[]){
int divide=sum(25,0);
System.out.println(divide);
System.out.println("Continuation of the program");
}
}

Output

Exception in thread "main" java.lang.ArithmeticException: Second parameter is 0

In this example, we are explicitly throwing an ArithmeticException.

Java throws keyword

In java throws keyword is used to mention the exceptions that might occur during the execution in the program.

throws keyword are used to handle checked exceptions

Syntax for throws keyword

accessModifier returnType methodName() throws ExceptionType1, ExceptionType2{
// code
}

If we use throws keyword then the exceptions declared must be handled where you are calling the method, using try-catch block

Example for throws Keyword

import java.io.*;
class ThrowsExample {
void Method(int num)throws IOException, ClassNotFoundException{
if(num==1)
throw new IOException("IOException Occurred");
if(num==2)
throw new ClassNotFoundException("ClassNotFoundException");
else
System.out.println("no exception occured");
}
}
public class exec3{
public static void main(String args[]){
try{
ThrowsExample obj=new ThrowsExample();
obj.Method(1); //check with 2 and 3
}catch(Exception e){
System.out.println(e);
}
System.out.println("normal flow...");
}
}

Output( When Method argument is 1)

java.io.IOException: IOException Occurred
normal flow

Output( When Method argument is 2)

java.lang.ClassNotFoundException: ClassNotFoundException
normal flow

Output( When Method argument not 1 or 2)

no exception occured
normal flow

IN the above example, we used throws keyword to handle two exceptions IOException, ClassNotFoundException

if the argument for the Method is 1, the output will be IOException Occurred, next normal flow of the program will be continued

if the argument for the Method is 2, then the output will be ClassNotFoundException, the next normal flow of the program will be continued

if the argument for the Method is not 1 or 2, then the output will be no exception occurred, next normal flow of the program will be continued

From the above example, we can understand that we can handle multiple exceptions using throws keyword

What if there is no try-catch block, but you have declared an exception

If you are not using a try-catch block to handle an exception, then you should declare a method with the specifying throws keyword with the exception type

Syntax

public static void main(String args[])throws ExceptionName

Example

import java.io.*;
class ThrowsExample {
void Method(int num)throws IOException{
if(num==1)
throw new IOException("IOException Occurred");
else
System.out.println("no exception occured");
}
}
public class exec3{
public static void main(String args[])throws IOException{ //must declare exception
ThrowsExample obj=new ThrowsExample();
obj.Method(1); //check with 2 also
System.out.println("normal flow...");
}
}
Output (When Method argument is 1)Exception in thread "main" java.io.IOException: IOException Occurred
Output (When Method argument is 1)no exception occured
normal flow...

if the argument for the Method is 1, the output will be IOException Occurred, next the program will be getting runtime exception error and it will be stopped, print statement will not be executed

if the argument for the Method, not 1, the output will be no exception occurred, next the program will be contained, so normal flow print statement will be executed

Difference between throw and throws in Java

ThrowThrows
Throw keyword is used throw only one exception explicitythrows keyword can be used for handlngg multiple exception
unchecked exceptions are used with throw keyowrdchecked exceptions are used only with throws
Throw is followed by an instance.Throws is followed by class.
Last updated on by vishal devxo