Skip to main content

Exception Handling Interview Questions in Java


Exception Handling




1.What is Exception Handling in Java?

Java provides Exception Handling mechanism to handle Runtime errors that occur in JVM. There are checked exceptions in a program that we expect to occur in certain situations. Exception handling mechanism catches these checked exceptions and takes relevant actions.

2.In Java, what are the differences between a Checked and Unchecked?

Checked Exceptions extend Throwable class, but they do not extend RuntimeException or Error classes. UncheckedException extend RuntimeException class. Checked Exceptions are checked at compile time in Java. Unchecked Exceptions happen at Runtime, so they are not checked at compile time. IOException, SQLException etc. are examples of Checked Exceptions. NullPointerException, ArithmeticException etc. are examples of Unchecked Exceptions.

3. What is the base class for Error and Exception classes in Java?

Error as well as Exception class is derived from Throwable class in Java.

4.What is a finally block in Java?

Java provides a finally block with a try block. This is an optional block. But finally block is always executed after the execution of try block.

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

As per Java specification, a finally block is always executed, whether an error occurs or not, whether an exception is handled or not. It helps in doing the cleanup like- Rollback Transaction, Close Connection, Close a file etc.

6.Can we create a finally block without creating a catch block?

Yes. A finally block can follow a try block or catch block. So we can defined a finally block just after a try block.

7.Do we have to always put a catch block after a try block?

Java does not enforce the rule to put a catch block after try block. We can write catch block or finally block after a try block. Any exception that we want to catch is mentioned in catch block.

8.In what scenarios, a finally block will not be executed?

There are two main scenarios in which finally block is not executed:
1.     Program exits by calling system.exit() call.
2.     A fatal error causes JVM to crash.

9. Can we re-throw an Exception in Java?

Yes, Java allows to re-throw an Exception.

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

Java provides throw keyword to throw an exception from a method or a static block. Java provides throws keyword to mention the probable exception thrown by a method in its declaration. We use throw to explicitly throw an exception. We used throws to declare an exception in method definition. We cannot propagate checked exceptions with throw only. But checked exceptions can be propagated with throws keyword. A throw call is followed by an instance. Class or Exception follows a throws keyword. Call to throw occurs within a method. throws is just used with method signature. We can throw only one exception at a time. But we can mention as many exceptions in throws clause.

11. What is the concept of Exception Propagation?

In Exception Propagation, uncaught exceptions are propagated in the call stack until stack becomes empty. This propagation is called Exception Propagation. Let say an exception propagates from one method to another method. A() calls B(), which calls C(), which calls D(). And if D() throws an exception, the exception will propagate from D to C to B to A, unless one of the methods catches the exception.

12. When we override a method in a Child class, can we throw an additional Exception that is not thrown by the Parent class method?

Yes, Java allows us to throw additional Exception in a child class, but the additional exception should be an unchecked exception (RuntimeException).



Comments

Popular posts from this blog

DOCKER Interview questions

DOCKER 1. What is Docker? Docker is Open Source software. It provides the automation of Linux application deployment in a software container. We can do operating system level virtualization on Linux with Docker. Docker can package software in a complete file system that contains software code, runtime environment, system tools, & libraries that are required to install and run the software on a server. 2. What is the difference between Docker image and Docker container? Docker container is simply an instance of Docker image. A Docker image is an immutable file, which is a snapshot of container. We create an image with build command. When we use run command, an Image will produce a container. In programming language, an Image is a Class and a Container is an instance of the class. 3. How will you remove an image from Docker? We can use docker rmi command to delete an image from our local system. Exact command is: % docker rmi <Image Id> If we want to fin...

Amazon Web Services(AWS) Interview questions

Amazon Web Services 1. What do you know about AWS Region? An AWS Region is a completely independent entity in a geographical area. There are two more Availability Zones in an AWS Region. Within a region, Availability Zones are connected through lowlatency links. Since each AWS Region is isolated from another Region, it provides very high fault tolerance and stability. For launching an EC2 instance, we have to select an AMI within the same region. 2. What are the important components of IAM? The important components of IAM are as follows: 1.     IAM User : An IAM User is a person or service that will interact with AWS. User can sign into AWS Management Console for performing tasks in AWS. 2.     IAM Group : An IAM Group is a collection of IAM users. We can specify permission to an IAM Group. This helps in managing large number of IAM users. We can simply add or remove an IAM User to an IAM Group to manage the permissions. 3. ...

Microservices Interview Questions

Microservices Interview Questions 1. What is a Microservice in Java? A Microservice is a small and autonomous piece of code that does one thing very well. It is focused on doing well one specific task in a big system. It is also an autonomous entity that can be designed, developed and deployed independently. Generally, it is implemented as a REST service on HTTP protocol, with technology-agnostic APIs. Ideally, it does not share database with any other service. 2. What are the benefits of Microservices architecture? Microservices provide many benefits. Some of the key benefits are: 1.      Scaling : Since there are multiple Microservices instead of one monolith, it is easier to scale up the service that is being used more. Eg. Let say, you have a Product Lookup service and Product Buy service. The frequency of Product Lookup is much higher than Product Buy service. In this case, you can just scale up the Product Lookup service to run on powerful ha...