Part 3 of the Java interview question series. In this, we will cover
Let's get started
Q31: What are exceptions? Types of Exceptions?
A31: Any event/problem that occurs during the execution of the program, due to which the normal flow of code cannot be attained is known as an Exception. It can occur at compile time or runtime.
There are 2 types of exceptions
- Checked Exception - The exceptions which occur at compile time and the compiler lets you know of a possible case where your code can fail or give an error. You won't be able to run your code until these are handled. The phenomenon of handling errors is known as Exception Handling. Eg: FileNotFoundException.
- Unchecked Exception - The exceptions which occur at the runtime, the compilation of the program will be a success. These usually occur because of bad logic or improper use of APIs. Eg: ArrayIndexOutOfBoundsException.
Class hierarchy
Q32: Can we write a try block without a catch block?
A32: Yes, we can write but then we need to add finally instead. We can have try block alone.
Q33: When finally block will not get executed?
A33: Whenever the program exits it can happen either when System.exit() is called or when JVM crashes or any other fatal error.
Q34: When we put a throw statement in Finally block, what will happen?
A34: As we know that finally blocks execute every time. The throw statement inside finally will take precedence over try and catch block throw statement.
Q35: Throw vs Throws?
A35: Thow - We can use throw keyword followed by Exception class. This will ask the program to explicitly induce that exception.
Throws - We can add throws keyword in the method signature followed by Exception class, it will tell the compiler that the given method might throw that particular exception mentioned.
Q36: How to make custom checked / unchecked exception?
A36: If you want to make a custom unchecked exception class then extend RuntimeException and for creating a custom checked exception class, extend Exception class.
Q37: Try with Resources
A 37: Try with Resources automatically closes the resource used within the try catch block. This can be done with Java 7 and above versions.
try (FileReader fr = new FileReader("file.txt")) { } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
It replaces the below code
FileReader fr = null; try { File file = new File("file.txt"); fr = new FileReader(file); } catch (IOException e) { e.printStackTrace(); } finally { try { fr.close(); } catch (IOException ex) { ex.printStackTrace(); } }
Q38: Exception Handling with Method Overriding in Java
A38: We would discuss 2 cases
- If the parent method does not declare an exception - The child overridden method cannot declare the checked exception but it can declare an unchecked exception.
- If the parent method declares an exception - The child overridden method can declare the same, subclass exception, or no exception but cannot declare an exception that has more precedence than the parent class method.
Case 1
import java.io.IOException; class Parent { void method() { } } class Child extends Parent { @override void method() { } } class Child1 extends Parent { /* @override void method() throws IOException { // 'method()' in 'Child1' clashes with 'method()' in 'Parent'; overridden method does not throw 'java.io.IOException' } */ } class Child2 extends Parent { @override void method() throws ArithmeticException { // No error as this is an unchecked exception } }
Case 2
import java.io.FileNotFoundException; import java.io.IOException; class Parent { void method() throws IOException { } } class Child extends Parent { @override void method() { } } class Child1 extends Parent { @override void method() throws FileNotFoundException { } } class Child2 extends Parent { @override void method() throws IOException { } } class Child3 extends Parent { /* @override void method() throws Exception { // error as Exception is superclass to IOException } */ }
#HappyLearning
Top comments (0)