Exception Handling - Checked Vs Unchecked Exceptions

There are two types of Exceptions

1. Checked Exceptions
2. Unchecked Exceptions

Checked Exceptions : These are the type of exceptions which are raised by the compiler for smooth execution of the code in the run time.

Lets take a simple real time scenario between Mother,Son and Lunch Box.

1. When the child is leaving to school, Mother asks the Son whether he has taken the lunch box with him,
 2. Son Checks his bag and tell his mother that he has taken the lunch box.

Here, The mother is checking the child because, if the he forget his lunch box, he would miss the meal at the lunch break.

If we represent it in java.

Mother - Java Compiler.
Student - Is the application.
Lunch Box - Is the parameter checked by the compiler, which may cause an issue.

Lets do a simple example, for Better understanding do not use an IDE for this example. 
package com.learning.exceptionhandling;

import java.io.PrintWriter;

public class SampleClass {

 public static void main(String[] args) {
  PrintWriter printWriter=new PrintWriter("c:/Sample.txt");
  printWriter.write("I am Bharani Ravi Kanth R");
 }
}

I am trying to print a text "I am Bharani Ravi Kanth R" to a file called Sample.txt. lets try compiling the code through command line.

C:\com\learning\exceptionhandling>javac SampleClass.java
SampleClass.java:8: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
                PrintWriter printWriter=new PrintWriter("c:/Sample.txt");
                                        ^
1 error

Here its not an issue with the application, but compiler is helping us to be cautious, What if the file you are trying to access doesn't exist at all. So the compiler is giving us the below message

unreported exception FileNotFoundException; must be caught or declared to be thrown
i.e, handle the code if the file we are trying to access doesn't exist.

There are two ways we can handle the checked exceptions,
1. try catch block.
2. throws keyword.

So in the above example, we can handle the code as below so that compiler compiles our code properly.

try catch block:

package com.learning.exceptionhandling;

import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class SampleClass {

 public static void main(String[] args) {
  try {
   PrintWriter printWriter = new PrintWriter("c:/Sample.txt");
   printWriter.write("I am Bharani Ravi Kanth R");
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
 }
}

throws keyword:

package com.learning.exceptionhandling;

import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class SampleClass {

 public static void main(String[] args) throws FileNotFoundException {
   PrintWriter printWriter = new PrintWriter("c:/Sample.txt");
   printWriter.write("I am Bharani Ravi Kanth R");
 }
}

Now let us compile the code.

C:\com\learning\exceptionhandling>javac SampleClass.java

C:\com\learning\exceptionhandling>

We can see now that compiler has no issues compiling the code.

Checked Exceptions are checked in two ways

1. Fully Checked: We need to check both parent and child,
2. Partial Checked: If we check only the parent not its child classes.


Note: Only possible partial checked exceptions classes in java are Throwable and Exception.


Unchecked Exceptions: The exceptions which are caused by the application, mostly at runtime,
Unchecked Exceptions are mostly

1. Errors
2. Runtime Exceptions.

Unchecked Exceptions - Error:  we are trying to run the application in the server, but the application couldn't start up because of OutOfMemory error, that is application is not having enough heap space to run. here its not the application which caused the issue, but the server doesn't have enough system resources to run the application.

Unchecked Exceptions - Runtime Exceptions: These are the exceptions which are caused by the applications at runtime, like dividing an integer by zero this causes ArithmeticException, trying to access an object which was never initialized causes null pointer exception.


Lets see a simple example, we are trying to fetch an value from an string array object which never initialized

package com.learning.exceptionhandling;

public class SampleClass {

 public static void main(String[] args) {

  String[] stringArray=null;
  System.out.println(stringArray[0]);
 }
}

Output:
Exception in thread "main" java.lang.NullPointerException
 at com.learning.exceptionhandling.SampleClass.main(SampleClass.java:8)


Note: Runtime exceptions are not found by the compiler while compiling our source code.


I am an open source enthusiast. Currently having 6 years of experience and part of Ericsson Global. I am a traveler, Coding manic, Foodie, Gaming Freak and an Amazing Cook.

Share this

Pages
Previous
Next Post »