Java Throw and Throws Keyword
Java Throw and Throws Keyword
In Java programming, there are situations where we might come across unexpected events such as a file not found, a network connection lost, invalid data entered by the user, etc. These events are known as exceptions. Java provides a robust and object-oriented way to handle these exceptions known as 'Java Exception Handling'. In this article, we will focus on two essential keywords, 'throw' and 'throws', used in exception handling.
Java Throw Keyword
The 'throw' keyword in Java is used to explicitly throw an exception from a method or any block of code. We can throw either checked or unchecked exceptions. The 'throw' keyword is mainly used to throw custom exceptions.
The general form of the 'throw' keyword is as follows:
throw instance;
Here, 'instance' must be an object of type Throwable
or a subclass of Throwable
.
Here is an example where 'throw' keyword is used to explicitly throw an exception:
public class ThrowExample {
static void validate(int age){
if(age<18)
throw new ArithmeticException("Person is not eligible to vote");
else
System.out.println("Person is eligible to vote");
}
public static void main(String args[]){
validate(13);
System.out.println("End of the program");
}
}
In the above example, an ArithmeticException is thrown if the age is less than 18. As a result, the program gets terminated and "End of the program" doesn't get printed.
Java Throws Keyword
The 'throws' keyword in Java is used to declare an exception. It gives information to the programmer that there may occur an exception so it is better for the programmer to provide the exception handling code so that a normal flow can be maintained.
The general form of 'throws' keyword is as follows:
type method-name(parameters) throws exception-list
{
//code
}
Here, 'exception-list' is a list of the exceptions that a method might throw.
Here is an example where 'throws' keyword is used:
import java.io.IOException;
class Testthrows1{
void m() throws IOException{
throw new IOException("Device error");//checked exception
}
void n() throws IOException{
m();
}
void p(){
try{
n();
}catch(Exception e){System.out.println("Exception handled");}
}
public static void main(String args[]){
Testthrows1 obj=new Testthrows1();
obj.p();
System.out.println("Normal flow");
}
}
In the above example, the method 'm()' throws an IOException. The method 'n()' invokes 'm()' and does not handle the exception, thus declaring it using the 'throws' keyword. The method 'p()' invokes 'n()' and handles the exception using a try-catch block. As a result, "Normal flow" gets printed.
Conclusion
The 'throw' and 'throws' keywords play a crucial role in handling exceptions in Java. 'throw' is used to trigger an exception where as 'throws' is used in the method signature to indicate that this method might throw the specified exceptions. Understanding these concepts is important to write robust and error-free Java code.
In the next articles, we will discuss more about Java exception handling, such as finally block and custom exceptions. Stay tuned and practice these concepts to become proficient in Java exception handling.