65 Java Exception Handling Interview Questions

41. Assume that statement 2 arises an exception in the following try-catch block:

try {
  statement 1;
  statement 2;
  statement 3;
}
catch (Exception1 e1) {
}
catch (Exception2 e2) {
}
statement 4;

Answer the following questions:
a) Will statement3 be executed?
b) If the exception is not handled in catch block, will statement 4 be executed?
c) If the exception is handled in the catch block, will statement 4 be executed?

Ans: a) No

b) No, program will be terminated abnormally if the exception is not caught in catch block.

c) Yes.

42. What is a finally block in Java?

Ans: A finally block is a block of code that is always executed whether an exception occurs within a try block or not.

43. What is the use of finally block in Java?

Ans: finally block or clause is used for freeing up resources, cleaning up code, db closing connection, io stream, for terminating threads, etc.

44: Can we create a finally block without creating a catch block?

Ans: Yes

45. In what scenarios or conditions, a finally block will not be executed?

Ans: a) When System.exit() method is invoked before executing finally block.
b) When an exception happens in the finally block.

46. What is the importance of finally block in exception handling?

Ans: Refer to answer of question 43.

47. What is the difference between finally block and finalize() method?

Ans: Finally block will be always executed whether an exception is thrown or not. So, it is used to free up resources.

finalize() is a protected method in the Object class which is called by the JVM just before an object is a garbage collected.

48. Does a finally block override the value returned by try or catch block?

Ans: Yes

49. What is throw in Java?

Ans: Throw in Java is a keyword that is used to throw a built-in exception or a custom exception explicitly.

50. How do we throw an exception in Java?

Ans: We can throw an exception (either checked or unchecked exceptions) using throw statement in a try block. When an exception occurs in the try block, throw keyword transfers the control of execution to the caller by throwing an object of exception.

51. Can we throw multiple exceptions in one throw statement?

Ans: No, we cannot throw multiple exceptions in one throw statement. Only one object of exception type can be thrown by using throw statement at a time.

52. Can we use throw statement inside static block?

Ans: Yes, we can use throw statement inside the static block provided that exception handling is present.

53. What is rethrowing an exception in Java?

Ans: When an exception occurs in a try block, it is caught by a catch block inside the same method. The same exception object out from catch block can be rethrown explicitly using throw keyword. This mechanism is called rethrowing of exception in Java.

54. Explain the throws clause in Java.

Ans: Throws clause in Java is used with a method declaration. It provides information to the caller method about exceptions being thrown and the caller method has to take the responsibility of handling the exception.

55. What is the difference between throw and throws in Java?

Ans: There are several differences between throw and throws keywords. A list of important differences between them are given below:

Throw ClauseThrows Clause
1. throw clause is used when we want to throw an exception explicitly and want to handle it using catch block.1. throws clause is used when we do not want to handle the exception and throw it out of the method.
2. Throw keyword is used inside the method body.2. Throws keyword is used in method declaration (signature).
3. Throw is always followed by an object of Exception.
e.g. throw new FileNotFoundException
3. Throws is always followed by name of exception class.
e.g. throws FileNotFoundException
4. Throw is used to throw only one exception at a time.4. Throws is used to throw multiple exceptions at a time.
e.g. public void method()throws IOException, SQLException

56. Can we re-throw an Exception in Java?
Or Can a catch block throw an exception caught by itself?

Ans: Yes, we can re-throw an exception from catch block to another class where it can be caught.

57. What is custom or user-defined exception in Java?

Ans: Custom exceptions in Java are those exceptions that are created by a programmer to meet their specific requirements of the application. With the help of user-defined exception, we can create our own exception and message.

58. How to create your own user-defined exception in Java?

Ans: Refer to this tutorial: User-defined Exception in Java

59. What is a chained exception in java?

Ans: Throwing an exception along with another exception is called chained exception in java.

60. What is Throwable in Java?

Ans: Throwable in Java is a class that is the superclass of all exceptions and errors which may occur in Java program. It extends object class.

61. What are the methods provided by Throwable class in Java?

Ans: An important list of methods provided by Throwable class is as follows:

  • getMessage()
  • toString()
  • printStackTrace()
  • fillInStackTrace()
  • getStackTrace()
  • getClause()

62. What is error in Java? What are the types of errors in Java programming?

Ans: Error in Java occurs when a programmer violates the rules of Java programming language. It might be due to programmer’s typing mistakes while developing a program.

There are three types of errors in Java. They are:

a. Compile-time errors (Syntax errors)
b. Runtime errors
c. Logical errors

63. What is the difference between error and exception in Java?

Ans: The difference between error and exception is as follows:

ExceptionError
1. Exception represents the problem that can be solved.1. Error represents the problem that cannot be solved.
2. Exceptions are related to the application.2. Errors are related to the environment in which the application is running.
3. Exceptions can be handled and can continue the execution of program.3. Errors cannot be handled.
4. Exceptions are further classified into checked and unchecked exceptions.4. Errors are not further classified into such classification.

64. State the difference between runtime error and syntax error (compile-time error).

Ans: The main difference between syntax error and runtime errors are as follows:

Syntax ErrorRuntime Error
1. A syntax error occurs when we violate any grammatical rule in a programming language.1. A runtime error occurs when a program terminates abnormally.
2. Syntax error can be found at compile time.2. Runtime error can be found only when we execute the program.
3. Syntax errors are those errors that are caused by incorrect usage of programming language.3. Runtime errors are those errors that are caused by incorrect usage of programming logic.

65. What is the difference between catch block and finally block?

Ans: The difference between catch block and finally block is as follows:

Catch blockFinally block
a. Catch block is used to handle an exception thrown by try block.a. Finally block is used to execute important code such as closing connection, io stream, etc.
b. Catch block is executed only when an exception occurs in the try block otherwise it is skipped.b. Finally block is always executed whether an exception occurs or not.
c. We can use multiple catch blocks for a single try block.c. Only one finally block can be used for a single try block.

66. Can a catch or finally block throw an exception?

Ans: Yes, catch or finally block can throw checked or unchecked exception but it should be handled accordingly.

67. Can we throw an exception explicitly or manually?

Ans: Yes, using throw clause.


Recommended Interview Questions


In this tutorial, we have covered almost all the important exception handling interview questions in Java with the best possible answers. I hope that this collection of exception handling interview questions will help you to understand what kind of interview questions can be asked in the interview. All the best!!!