Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Tuesday 29 August 2017

Interview Questions on Exception Handling in Java

Java Exception Handling Interview Questions

java exception handling interview questions

Now, Here we are going to cover the most important topic from java interview point of view is the interview questions on exception handling in java.

There are many exception handling interview questions in java which is mostly asked in any java interviews.

So without any delay, let's start.


(1) What is an exception?

Exception means "abnormal condition", Exception is an abnormal condition that arises during the execution of the program and it disrupts the normal flow of the program. In other words, Exceptions are the objects and represents the logical errors that occur at run-time.


(2)  What is exception handling in java?

8 Exception handling means handle the exception by using some handler and maintain the normal flow of the programs. Exception handling is mainly used to handle checked exception.

(3) How can you handle an exception in java?

There are 3 ways we can handle the exception. We can use three handler - try, catch and finally block to handle the java exception.

(4) What is try-catch in exception handling?

In java try and catch is a most important block or handler which is used to handle the exceptions and finally block also used.

try Block: - Doubted statements or code i.e that can throw an exception, kept in this block.

catch Block: - This block is used to catch or handle the exception which is thrown from the try block.


(5) In which java package, Exceptions are defined?

Exception definition is defined in java.lang package.

(6) Which are the top most or super class for exception and error?

Throwable is the super or top most class for exception and error.


(7) What is finally in java?

Finally is a block in java which always executes whether the exception occurs or not and whether exception handled or not.

(8) Can we use try block without a catch or finally block?

No, We can't use try block without a catch or finally block.

(9) Can we use try block without catch block?

Yes, We can use try block without catch block but with finally block.

(10) Can we use multiple catch block with single try block?

Yes, We can.

(11) Can we use try, catch and finally together?

Yes, We can use try, catch and finally together in an exception handling program but finally block must be the last block.


(12) Can we use try block within another try block?

Yes, We can use try block into another try block. It is called nested try block.

(13) How many types of exception are there in exception handling?

There are two types of exception (1) Checked Exception and (2) Unchecked Exception.

(14) What is the difference between checked and unchecked exception?

Checked Exception: - Checked exception is an exception which is caught by the compiler at compile time is called checked exception e.g IOException, SQLException, etc.

Unchecked Exception: - Unchecked exception is a exception which occurs at run-time e.g ArithmeticException, NullPointerException and ArrayIndexOutOfBoundsException.


(15) What is the difference between exception and error?

Error: - This is a system issue and it happens at run-time.

Exception: - This is a wrong logical error in programs and can occur at compile-time or run-time.

(16) Can we keep other statements in between try, catch and finally block?


No, we can't.

For example:

class Demo
{
public static void main(String args[])
{
try
{
int i = 44/0;
System.out.println(i);
}
System.out.println("first");//compile-time error
catch(Exception e)
{
System.out.println(e);
}
System.out.println("second");//ct error
finally
{
System.out.println("finally block");
}
}
}


(17) Can we write multiple exceptions in single catch block?

Yes, It is 100% possible.

For example:

class Test
{
public static void main(String args[])
{
try
{
//code;
}
catch(Exception | ArithmeticException e)
{
//code;
}
}
}

(18) Difference between throw and throws keyword in java?


throw Keyword

  • throw keyword is basically used for custom exception and it is used to explicitly throw an exception.
  • throw is used within the method.
  • It is followed by an instance.
  • By the help of throw keyword, we cannot forward checked exception in calling chain.

throws Keyword:
  • It is used to declare an exception.
  • It is used with the method signature.
  • It is followed by a class.
  • By the help of throws keyword we can forward checked exception in calling chain.

(19) Difference between final, finally and finalize?


(20) When finally block does not get executed?

If we call System.exit() then finally block will not execute.

(21) What is the custom exception?

Custom exception is also called user define exception when user creates own exception is called a custom exception. By extending Exception class we can create the custom exception.

(22) Can we write return statements in try or finally block?

Yes, we can write return statements in both try and finally block.

For example:

int add()
{
try
{
return 30;
}

Visit Other Java Interview Articles, link given below.

Java Multithreading Interview Questions.
Servlet Interview Questions.
JSP Interview Questions.

I hope, this exception handling interview questions and answers post will help you to make java interview easy.

Share:

2 comments:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate