Java Exceptio Handling Interview Questions

1) Explain an exception?

The logical errors are represented by the objects called as exceptions that occur during run time and happens during run time and it enters into the ambiguity state.

JVM creates the objects automatically for representing these run time errors called as Exceptions.

2) Explain the difference between an error and an exception?

Serious problems are indicated by an error called subclass of throwable that is a reasonable application must not try to catch.

A subclass of Throwable is called an error that shows the serious problems of a reasonable application must not try to catch. There are lots of errors with strange conditions.

3) Explain the way exceptions are handled in Java?

With the help of try catch blocks, exceptions can be handled.

For generating the exceptions, the statements need to have try block because when there is an exception shown in a try block it will be assigned with the next exception class object in catch block.

Read More :  Do you know about Exception Handling in Java?

4) Explain the super class of Error and Exception

Throwable.

5.Exceptions are defined in which java package

java.lang.Exception

6.Explain throw keyword in java?

An exception is thrown manually with the help of throw keyword.

For suspending the execution, whenever needed the functionality relies on the user-defined logical condition for throwing the keyword to throw the exception.

7. Is it possible to have try block without catch block?

You can use try block without catch block by using finally block

Try with finally block is assisted by Java

It is quite familiar that block will get to know when there is an exception that happened in a try block, Except System. Exit () will have its execution always.

8. Explain unreachable blocks in Java

Unreachable blocks are regarded as block of statements to which the control cannot reach in any case.

Java doesnot support unreachable blocks.

Read More :  What is Exception Handling in Java?

9. Explain user defined exception or custom exception in Java

Custom exceptions can be defined by us over here by extending the Exception class

A constructor must be written by us for passing the message.

User defined exception in Java.

10. Explain the various ways of print exception message on console

In Java there are three ways to find the details of the exception.

They are:

1. Using an object of java.lang.Exception

2. Using public void printStackTrace() method

3. Using public String getMessage() method.

11. Is it possible to write return statement in try and catch blocks

It is possible to write return statement of the method in try and catch block

For using it there is a need to follow few rules.

12. In finally block is it possible to write return statement?

In finally block, there is a possibility to write return statement of the method.

Read More :  Java Exceptions Handling

13. Explain the difference between throw and throws

User defined exceptions are thrown with the help of throw keyword.

The information about unhandled exceptions of the method to the end user is done with the help of throws keyword and it can explicitly offer this kind of an information.

14. Is it possible for us to change an exception of a method with throws clause from unchecked to checked while overriding it?

Not possible

In sub class, if super class method throws exception then you need to mention the throws with the same class or the sub class exception.

Therefore it is not possible to change from unchecked to checked.

15. Explain the rules for following the overriding in super class method for throwing the exception

If checked exception is thrown by the sub class then the super class must throw the same or super class exception of this.

If checked or unchecked exceptions are thrown by the super class method then it is not necessary to put throws in a sub class overridden method.

In sub class, if super class method throws exceptions then you need to mention the throws for using the same class or its sub class exception.