Checked Exception
Checked exceptions are particular types of exceptions that are checked at compile time. This means that if a method is throwing an exception, it should be handled using a try-catch block, or this can be operated using the throws keyword. The code will show a compilation error if this is not dealt with.
Example
Code
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class Ex12 {
public static void method() throws FileNotFoundException {
FileReader file = new FileReader("C:\\Users\\MS\\Desktop\\answer.docx");
BufferedReader fileInput = new BufferedReader(file);
throw new FileNotFoundException();
}
//main method
public static void main(String args[]){
try
{
method();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
System.out.println("rest of the code...");
}
}
You can also try this code with Online Java Compiler
Run Code
Output
java.io.FileNotFoundException
at CODING/CN.Ex12.method(Ex12.java:15)
at CODING/CN.Ex12.main(Ex12.java:22)
rest of the code...
You can also try this code with Online Java Compiler
Run Code
In this code, we have created two separate functions one is for throwing an exception and the other for catching an exception. In the first method, we simply used the file reader function to fetch some files after we read that file and then throw some exceptions. The next function uses a try-catch exception to catch that function. If we found the file, it will print the stack trace with the help of a StackTrace function. Then it will move again as it is.
Must Read Static Blocks In Java.
Unchecked Exception
Unchecked Exceptions are those which are not checked during compile time. The compiler unchecks all types of exceptions. Its the duty of the programmer to specify the catch exception or specify it. In Java exceptions under Error and RunTimeException classes, you can find the unchecked exceptions.
Example
Code
public class Code1 {
// Main driver method
public static void main(String args[])
{
int x = 0;
int y = 10;
int z = y / x;
}
}
You can also try this code with Online Java Compiler
Run Code
Output
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Code1.main(Code1.java:8)
When run, the given program throws ArithmeticException. Since ArithmeticException is an unchecked exception, the compiler allowes it to compile.
You can practice by yourself with the help of online java compiler.
Frequently Asked Questions
-
What is the use of the throw keyword?
The throw keyword is used to throw an explicitly single exception. It can be expressed in the form of a method and also in the form of a block of code. In this case, both checked and unchecked exceptions can be thrown using the throw keyword.
-
What is an exception list?
It is a part of the logical message tree in which the message flow writes information about the exceptions that might occur during the processing of the message.
Key Takeaways
In this blog, we have covered the throw keyword. We have covered a brief introduction of the throw keyword, a Java throw keyword along with its syntax, and the use of the java throw keyword. We also took a few examples of using the throw keyword in different scenarios.
If you want to learn more about exception handling, you can visit our website, Exception Handling.