throw-and-throws-keyword
throw and throws are the different keywords in Java, used in exception handling and both keywords have their own functionality in Java.
throw Keyword in Java
Key Point
- The throw keyword is used to throw an object of throwable type in the code.
- throw keyword must be used inside the method body or constructor body.
- we can throw only one throwable type of object in the code using the throw keyword.
Syntax:
throw new ArithmeticException();
ArithmeticException e1=new ArithmeticException(); throw e1;
throw new String();
Example: Using throw in Java
package com.quipoin;
public class ThrowKeyword {
static void divide(int n1,int n2) {
if(n2==0) {
throw new ArithmeticException("Denominator cant be zero");
}
else {
System.out.println("dividing "+n1+" by "+n2);
int res=0;
res=n1/n2;
System.out.println("Result is "+res);
}
}
public static void main(String[] args) {
try {
divide(12, 0);
}
catch (ArithmeticException e) {
System.out.println("Printing exception details");
e.printStackTrace();
}
}
}
Output:
Printing exception details
java.lang.ArithmeticException: Denominator cant be zero
at com.quipoin.ThrowKeyword.divide(ThrowKeyword.java:6)
at com.quipoin.ThrowKeyword.main(ThrowKeyword.java:17)
How throw Works Internally
- Exceptions are always thrown at runtime.
- whenever an unchecked exception category object is thrown, the runtime system looks for the handler in the current method, if not looks for the handler in the caller method.
- If the execution stack if no handler, the entire stack will collapse and the data will be lost is known as stack unwinding.
- Whenever the throw statement throws a checked exception category object, the JVM looks for the handler in the current method body if not found compiler throws an unhandled error.
throws Keyword in Java
The throw keyword is used in method declarations to indicate that a method may throw exceptions.
- throws keyword is used to propagate an exception to its caller function.
- It must be declared in the method signature(declaration).
- By using the throws keyword we can propagate multiple exceptions to the caller method.
Syntax:
void test() throws SQLException;
The handler for the checked exception is defined in the caller function body, the exception propagation is done by using the throws declaration statement
Example:
package com.quipoin;
import java.io.File;
import java.io.IOException;
public class ThrowsKeyword {
void create(String path) throws IOException{ //called function body
File f1=new File(path);
f1.createNewFile();
}
public static void main(String[] args) { //caller function body
System.out.println("Main method started");
ThrowsKeyword t1=new ThrowsKeyword();
try {
t1.create("c:\\keshava\\JECM84\\book.txt");
} catch (IOException e) {
System.out.println("Handling exception due to:"+e.getMessage());
e.printStackTrace();
System.out.println("Main method ended");
}
}
}
Output:
Main method started
Handling exception due to:The system cannot find the path specified
Main method ended
java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(Unknown Source)
at com.quipoin.ThrowsKeyword.create(ThrowsKeyword.java:9)
at com.quipoin.ThrowsKeyword.main(ThrowsKeyword.java:15)
Example: Handling Multiple Exceptions
package com.quipoin;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
public class FileCreation {
void create(String path) throws IOException,SQLException{
File f1=new File(path);
f1.createNewFile();
}
public static void main(String[] args) {
System.out.println("Main method started");
FileCreation fc=new FileCreation();
try {
fc.create("c:\\keshava\\JECM84\\book.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Output:
Main method started
java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(Unknown Source)
at com.quipoin.FileCreation.create(FileCreation.java:9)
at com.quipoin.FileCreation.main(FileCreation.java:15)
Difference Between throw and throws
Feature | throw | throws |
---|---|---|
Definition | used to manually throw an exception | Used to declare exceptions a method may throw |
Usage | Used inside a method or constructor body | Used in method signature |
Type | Used to throw a single exception | Can declare multiple exceptions |
Example | throw new ArithmeticException(); | void method() throws IOException, SQLException; |
Handling | The thrown exception must be handled within the method or propagated | The exception handling is transfer to the calling method |
Two Minute Drill
- throw is used to throw an exception manually within a method.
- throws is used to declare exceptions in a method signature and propagate them to the caller.
- If an exception is done , the compiler forces to handle it using try-catch or throws.
- If an exception is unchecked, handling is optional, but failure to do so may lead to program termination.