Java programming language provides a robust error handling mechanism and is a core part of Java programming language. Proper and efficient error handling is required for any stable Java program. Java exceptions interview questions are very frequently asked both in telephonic screening interviews as well as in face-to-face interviews; on topics such as exceptions vs errors, runtime vs compile time errors, checked exceptions vs unchecked exceptions etc.
Below Java exception interview questions, answers, tips and samples will refresh your knowledge of error handling in Java.
Important keywords on Java exceptions are provided at the end of the questions. Review them, make them a part of Java vocabulary, and talk about them confidently during your interview process.
Java exceptions are problematic events that occur during the execution of Java programs, that disrupt the normal flow program. Examples - A java program that has to reads a file but the file does not exist in the file system during program execution,
A java program that tries to get an object from an array at an index position that is greater than the array
In java programing language exceptions are objects of type Exception. The exception object wraps details of the problem such as type of problem, state of program when the problem occurred etc. When an exception happens within a method, an exception object is created send to the Java runtime system
Creating an exception object and handing it over to the runtime system is termed as throwing an exception. A method hands over an exception to the Java runtime system by using the keyword throw followed by exception object. This method must also declare that it throws an exception by using the keyword throws in the method declaration followed by the type of exception that is thrown.
Java programming language provides three exception handling components - try block, catch block and finally block - that facilitate the handling of exceptions in java programming language.
1. Any Java code that may throw an exception can be enclosed within the try block.
2. An exception handler is tied to the try block in the form of the catch block which follows the try block. The catch block catches the exception, and handles or processes the exception.
3. finally block follows the catch block. The code in finally block always executes, irrespective of whether an exception is thrown in the try block or not. finally block is commonly used to close resources that were opened within the try block.
There are two kinds of exceptions in Java programming language - checked exceptions and unchecked exceptions.
Checked exceptions are compile time exceptions, i.e. the exceptions occur during compile time of the program, and the program has to handle these exceptions before it can be compiled.
Unchecked exceptions are run time exceptions, i.e the exception occurs during the program runtime.
Exceptions are problematic events that are caused by user or programmatic error. Exceptions are typically handled by the Java program in order to prevent abnormal termination of the program
Errors are problematic events that are beyond the control of user or programmer. Errors are not usually handled within the Java program
Yes, you can have multiple catch blocks in Java programming language. Each catch block handles a specific exception. The catch block specified first must catch an exception that is lower in the exception hierarchy, than an exception caught by the lower catch block.
Yes, In Java SE 7 and later, a single catch block can handle more than one type of exception.
throws is a keyword used in Java programming language that indicates that a method may return one or more specific types of exceptions...
*** See complete answer in the Java Interview Guide.
finally block is a block of code that is defined either after a try and catch block, or after a try block that is always executed.
*** See complete answer in the Java Interview Guide.
Yes you can have a finally block without the catch block...
*** See complete answer in the Java Interview Guide.
try-with-resource is a try block that declares and initializes one or more resources...
*** See complete answer in the Java Interview Guide.