Thursday, December 13, 2018

Difference Between checked Exception and Unchecked Exception in Java

In this post you will learn what is checked and unchecked Exceptions and difference between them. The following are the differences between them.

Checked: are the exceptions that are checked at compile time. If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using throws keyword. 

Example: FileNotFoundException, EndOfFileException etc.

So the compiler at compile time will check if a certain method is throwing any of the checked exceptions or not,if yes it will check whether the method is handling that exception either with “Try&Catch” or “throws”,if in case the method is not providing the handling code then compiler will throw error saying “Unreported Exception”

For example

import java.io.*
class Example
{
public static void main(String[] args)
 {
PrintWriter pw=new PrintWriter("xyz.txt"); //creating a new printwriter object to write in a file named "xyz.txt"
pw.println("Hello World");
 } }

The above snippet is supposed to print “Hello World” in file named “xyz.txt”

There may be a chance of file “xyz.txt” is not present in specified directory,so the compiler will check if any handling code is provided in case file is not present

In above snippet handling code is not provided either with “Try&Catch” or “throws” so the compiler will throw error

The same example with some modification:

import java.io.*
Class Example
{
public static void main(String[] args) throws FileNotFoundException
 {
PrintWriter pw=new PrintWriter("xyz.txt"); //creating a new printwriter object to write in a file named "xyz.txt"
pw.println("Hello World");
}}
In this example handling code is provided with “throws” so compiler will not throw any error.



Unchecked Exceptions:

There are some exceptions which do not occur regularly in a program,and compiler will not check for those exceptions,these kind of exceptions are called Unchecked Exceptions

Example: ArithmeticException, NullPointerException etc

For example:

class Example{
public static void main(String[] args){
System.out.println(10/0); //Arithmetic Exception
}
The above program should throw “Arithmetic Exception” as division with “0” is not allowed

In this case the program compiles fine because compiler will not check for “Unchecked Exceptions” but the program will throw error at “Run Time” as division with “0” is illegal.

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...