Java - Exceptions - Interview Questions

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.

What are exceptions in Java programming language?

 FAQKey Concept

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.

How do you handle exceptions in Java programming language?

 FAQKey Concept

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.

What is the difference between checked exceptions and unchecked exceptions in Java programming language?

 FAQKey Concept

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.

What is the difference between Exceptions and Errors in Java programming language?

 FAQKey Concept

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

Can you have multiple catch blocks in Java programming language?

 FAQKey Concept

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.

Java Interview Guide has over 250 REAL questions from REAL interviews. Get the guide for $15.00 only.
 
BUY EBOOK
 

Can you catch more than one type of exception in a single catch block?

 

Yes, In Java SE 7 and later, a single catch block can handle more than one type of exception.

What is the difference between throws and throw in Java programming language?

 FAQKey Concept

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.

What do you understand by finally block in Java programming language?

 FAQKey Concept

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.

Can you have try and finally block without the catch block Java programming language?

 FAQKey Concept

Yes you can have a finally block without the catch block...

*** See complete answer in the Java Interview Guide.

What do you understand by try-with-resources in Java programming language?

 FAQKey Concept

try-with-resource is a try block that declares and initializes one or more resources...

*** See complete answer in the Java Interview Guide.

Java Interview Guide has over 250 REAL questions from REAL interviews. Get the guide for $15.00 only.
 
BUY EBOOK
 
 
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
 

Java - Interview Questions

Java - Object Oriented ProgrammingJava - Objects & ClassesJava - Data TypesJava - VariablesJava - StringsJava - ArraysJava - CollectionsJava - ReflectionJava - Lambda ExpressionsJava - StreamsJava - GenericsJava - ExceptionsJava - IOJava - ThreadsJava - ConcurrencyJava - JDBCJava - NetworkingJava - SecurityJava - JVM InternalsJava - PerformanceJava - New in Java 8Java - New in Java 9Java - New in Java 10Java - New in Java 11
 
MASTER Java  

Top ranked courses to help you master Java skills.
Java Programming Masterclass

iconicon

Offered By - Tim Buchalka
Platform - Udemy
Rating - * * * * *
Students Enrolled - 575,000 +

RECOMMENDED RESOURCES
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example