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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.vidvaan.corejava.exception17.common.checked.exceptions; import java.io.FileWriter; import java.io.IOException; // common checked exception example : FileNotFoundException public class FileNotFoundExceptionDemo { public static void main(String[] args) { FileWriter fw = null; try { fw = new FileWriter("C:\\fileNotFound.txt"); fw.write("Welcome to Vidvaan"); } catch (Exception e) { System.out.println(e); } finally { try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.vidvaan.corejava.exception17.common.checked.exceptions; import java.text.ParseException; import java.text.SimpleDateFormat; //common checked exception example : ParseException public class ParseExceptionDemo { public static void main(String[] args) { try { new SimpleDateFormat("MM, dd, yyyy").parse("02 02 2020"); } catch (ParseException e) { e.printStackTrace(); } } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package com.vidvaan.corejava.exception17.common.checked.exceptions; import java.util.concurrent.TimeUnit; //common checked exception example : InterruptedException public class InterruptedExceptionDemo { public static void main(String[] args) { // Create a new thread. Thread thread = new SampleThread(); // Start the thread's execution. thread.start(); // Interrupt the thread. thread.interrupt(); } } class SampleThread extends Thread { @Override public void run() { try { /* Sleep for some seconds. */ TimeUnit.SECONDS.sleep(10); } catch (InterruptedException ex) { System.err.println("An InterruptedException was caught: " + ex.getMessage()); } } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.vidvaan.corejava.exception17.common.checked.exceptions; //common checked exception example : ClassNotFoundException public class ClassNotFoundExceptionDemo { private static final String CLASS_DOES_NOT_EXIST = "com.mysql.jdbc.InvalidDriverClass"; public static void main(String[] args) throws Exception { System.out.println("Trying to load class which doesn't exist"); Class.forName(CLASS_DOES_NOT_EXIST); } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.vidvaan.corejava.exception17.common.checked.exceptions; import java.net.MalformedURLException; import java.net.URL; //common checked exception example : MalformedURLException public class MalformedURLExceptionDemo { public static void main(String[] args) { try { new URL("http://vidvaan.com"); // OK new URL("htp://vidvaan.com"); // MalformedURLException } catch (MalformedURLException e) { e.printStackTrace(); } } } |
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)