What are the two types of exceptions in Java which are the differences between them?
43. What are the two types of Exceptions in Java ? Which are the differences between them ?Java has two types of exceptions: checked exceptions and unchecked exceptions. Unchecked exceptions do not need to be declared in a method or a constructor’s throws clause, if they can be thrown by the execution of the method or the constructor, and propagate outside the method or constructor boundary. On the other hand, checked exceptions must be declared in a method or a constructor’s throws clause. See here for tips on Java exception handling. Show 44. What is the difference between Exception and Error in java ?Exception and Error classes are both subclasses of the Throwable class. The Exception class is used for exceptional conditions that a user’s program should catch. The Error class defines exceptions that are not excepted to be caught by the user program. 45. What is the difference between throw and throws ?The throw keyword is used to explicitly raise a exception within the program. On the contrary, the throws clause is used to indicate those exceptions that are not handled by a method. Each method must explicitly specify which exceptions does not handle, so the callers of that method can guard against possible exceptions. Finally, multiple exceptions are separated by a comma. 45. What is the importance of finally block in exception handling ?A finally block will always be executed, whether or not an exception is actually thrown. Even in the case where the catch statement is missing and an exception is thrown, the finally block will still be executed. Last thing to mention is that the finally block is used to release resources like I/O buffers, database connections, etc. 46. What will happen to the Exception object after exception handling ?The Exception object will be garbage collected in the next garbage collection. 47. How does finally block differ from finalize() method ?A finally block will be executed whether or not an exception is thrown and is used to release those resources held by the application. Finalize is a protected method of the Object class, which is called by the Java Virtual Machine (JVM) just before an object is garbage collected. There are two types of exceptions: checked exception and unchecked exception. In this guide, we will discuss them. The main difference between checked and unchecked exception is that the checked exceptions are checked at compile-time while unchecked exceptions are checked at runtime. What are checked exceptions?Checked exceptions are checked at compile-time. It means if a method is throwing a checked exception then it should handle the exception using try-catch block or it should declare the exception using throws keyword, otherwise the program will give a compilation error. Lets understand this with the help of an example: Checked Exception ExampleIn this example we are reading the file import java.io.*; class Example { public static void main(String args[]) { FileInputStream fis = null; /*This constructor FileInputStream(File filename) * throws FileNotFoundException which is a checked * exception */ fis = new FileInputStream("B:/myfile.txt"); int k; /* Method read() of FileInputStream class also throws * a checked exception: IOException */ while(( k = fis.read() ) != -1) { System.out.print((char)k); } /*The method close() closes the file input stream * It throws IOException*/ fis.close(); } } Output: Exception in thread "main" java.lang.Error: Unresolved compilation problems: Unhandled exception type FileNotFoundException Unhandled exception type IOException Unhandled exception type IOException Why this compilation error? As I mentioned in the beginning that checked exceptions gets checked during compile time. Since we didn’t handled/declared the exceptions, our program gave the compilation error. Method 1: Declare the exception using throws keyword. import java.io.*; class Example { public static void main(String args[]) throws IOException { FileInputStream fis = null; fis = new FileInputStream("B:/myfile.txt"); int k; while(( k = fis.read() ) != -1) { System.out.print((char)k); } fis.close(); } } Output: Method 2: Handle them using try-catch blocks. import java.io.*; class Example { public static void main(String args[]) { FileInputStream fis = null; try{ fis = new FileInputStream("B:/myfile.txt"); }catch(FileNotFoundException fnfe){ System.out.println("The specified file is not " + "present at the given path"); } int k; try{ while(( k = fis.read() ) != -1) { System.out.print((char)k); } fis.close(); }catch(IOException ioe){ System.out.println("I/O error occurred: "+ioe); } } } This code will run fine and will display the file content. Here are the few other Checked Exceptions –
What are Unchecked exceptions?Unchecked exceptions are not checked at compile time. It means if your program is throwing an unchecked exception and even if you didn’t handle/declare that exception, the program won’t give a compilation error. Most of the times these exception occurs due to the bad data provided by user during the user-program interaction. It is up to the programmer to judge the conditions in advance, that can cause such exceptions and handle them appropriately. All Unchecked exceptions are direct sub classes of RuntimeException class. Lets understand this with an example: Unchecked Exception Exampleclass Example { public static void main(String args[]) { int num1=10; int num2=0; /*Since I'm dividing an integer with 0 * it should throw ArithmeticException */ int res=num1/num2; System.out.println(res); } } If you compile this code, it would compile successfully however when you will run it, it would throw class Example { public static void main(String args[]) { int arr[] ={1,2,3,4,5}; /* My array has only 5 elements but we are trying to * display the value of 8th element. It should throw * ArrayIndexOutOfBoundsException */ System.out.println(arr[7]); } } This code would also compile successfully since class Example { public static void main(String args[]) { try{ int arr[] ={1,2,3,4,5}; System.out.println(arr[7]); } catch(ArrayIndexOutOfBoundsException e){ System.out.println("The specified index does not exist " + "in array. Please correct the error."); } } } Output: The specified index does not exist in array. Please correct the error. Here are the few unchecked exception classes:
What are the two types of exceptions in Java?There are mainly two types of exceptions in Java as follows: Checked exception. Unchecked exception.
What are the different types of exceptions?Exceptions can be categorized into two ways:. Built-in Exceptions. Checked Exception. Unchecked Exception.. User-Defined Exceptions.. What are exceptions two exception types?There are mainly two types of exceptions: checked and unchecked. An error is considered as the unchecked exception.
What is difference between exception?Both exceptions and errors are the subclasses of a throwable class. The error implies a problem that mostly arises due to the shortage of system resources. On the other hand, the exceptions occur during runtime and compile time.
|