English, asked by rishilaugh, 1 year ago

difference between checked and unchecked exception in java

Answers

Answered by nobita41
3
Unchecked: “Error” and its subclasses,”RunTimeException” and its subclassesChecked: Every exception other than Unchecked exceptionChecked Exception: These are the exceptions which may occur regularly in a program and compiler will check for those exceptions at “Compile Time” ,those exceptions are called Checked ExceptionsExample: 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 exampleimport 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 presentIn above snippet handling code is not provided either with “Try&Catch” or “throws” so the compiler will throw errorThe 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 ExceptionsExample: ArithmeticException, NullPointerException etcFor 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 allowedIn 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.P.S: All the exceptions occurs at “Run Time” only.writing this line because many people think that “Checked Exceptions” occurs at Compile time and “Unchecked Exceptions” occurs at Run time,which is not the case.
Answered by Anonymous
0

Answer:

hey \: pls \: like \: my \: answer

the checked exceptions are checked at compile-time .

unchecked exceptions are checked at runtime.

Similar questions