Java Common Checked Exceptions

There are some common checked exceptions occur in day to day java programming. Here we are going to see scenarios when those checked exceptions may occur.

1) FileNotFoundException

Thrown when pathname is wrong or not having access to the given pathname or pathname could be a folder … etc.

Output :

Exception in thread “main” java.io.FileNotFoundException: C:\fileNotFound.txt (Access is denied)
java.lang.NullPointerException: Cannot invoke “java.io.FileWriter.close()” because “fw” is null
at com.vidvaan.corejava.exception17.common.checked.exceptions.FileNotFoundExceptionDemo.main(FileNotFoundExceptionDemo.java:18)

2) ParseException

Thrown when something wrong either with the input String you are providing to the parse() method, or with the Format you are providing

Output :

java.text.ParseException: Unparseable date: “02 02 2020”
at java.base/java.text.DateFormat.parse(DateFormat.java:396)
at com.vidvaan.corejava.exception17.common.checked.exceptions.ParseExceptionDemo.main(ParseExceptionDemo.java:10)

3) InterruptedException 

Thrown when a thread is waiting, sleeping, or otherwise paused for a long time and another thread interrupts it using the interrupt method of Thread class.

Output :

An InterruptedException was caught: sleep interrupted

4) ClassNotFoundException

Thrown when JVM fails to load the specified class.

The following example tries to load a class “com.mysql.jdbc.InvalidDriverClass” using the Class.forName() method. However the specified class name cannot be found and thus a ClassNotFoundException is thrown.

Output :

Trying to load class which doesn’t exist
Exception in thread “main” java.lang.ClassNotFoundException: com.mysql.jdbc.InvalidDriverClass
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:340)
at com.vidvaan.corejava.exception17.common.checked.exceptions.ClassNotFoundExceptionDemo.main(ClassNotFoundExceptionDemo.java:9)

5) MalformedURLException

Thrown when the built-in URL class encounters an invalid URL

Output :

java.net.MalformedURLException: unknown protocol: htp
at java.base/java.net.URL.(URL.java:679)
at java.base/java.net.URL.(URL.java:568)
at java.base/java.net.URL.(URL.java:515)
at com.vidvaan.corejava.exception17.common.checked.exceptions.MalformedURLExceptionDemo.main(MalformedURLExceptionDemo.java:11)

Scroll to Top