Exception Handling Interview Questions in Java


Prev Tutorial Next Tutorial

Interview Questions on Exception Handling in Java

What is Exception Handling ?

The process of converting system error messages into user friendly error message is known as Exception handling.

What is Exception ?

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's Instructions.

Which is super class for any Exception class ?

Object class is super class for any Exception class.

Can any statement is possible between try and catch block ?

Each and every try block must be immediately followed by catch block that is no intermediate statements are allowed between try and catch block.

Can any try block contain another try block ?

Yes, One try block can contains another try block that is nested or inner try block can be possible.

Can finally block be used without catch ?

Yes but should follow "try" block then.

When IOException is thrown ?

IOException is thrown in following conditions which is given below;

  • Try to transfer more data but less data are present.
  • Try to read data which is corrupted
  • Try to write but file is read only.

When ArithmeticException is thrown ?

The ArithmeticException is thrown when integer is divided by zero or taking the remainder of a number by zero. It is never thrown in floating-point operations.

Difference between throw and throws ?

throwthrows
1throw is a keyword used for hitting and generating the exception which are occurring as a part of method bodythrows is a keyword which gives an indication to the specific method to place the common exception methods as a part of try and catch block for generating user friendly error messages
2The place of using throw keyword is always as a part of method body.The place of using throws is a keyword is always as a part of method heading
3When we use throw keyword as a part of method body, it is mandatory to the java programmer to write throws keyword as a part of method headingWhen we write throws keyword as a part of method heading, it is optional to the java programmer to write throw keyword as a part of method body.

Difference between checked Exception and un-checked Exception ?

Checked ExceptionUn-Checked Exception
1checked Exception are checked at compile timeun-checked Exception are checked at run time
2for checked Exception Extend Throwable class except RuntimeException.for un-checked Exception extend RuntimeException.
3e.g.
IOException, SQLException, FileNotFoundException etc.
e.g.
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException, NumberNotFoundException etc.

Is it necessary that each try block to be followed by catch block ?

It should be followed by either catch or finally block.

Explain throw, throws , try and catch in Java ?

throw: It is used to re throw an exception.

throws: It is used to declare that the method throws the respective exceptions.

catch: It is used to catch the exception that has been thrown by the respective try block.

Difference between Error and Exception

ErrorException
1Can be handle.Can not be handle.
2Example:
NoSuchMethodError
OutOfMemoryError
Example:
ClassNotFoundException
NumberFormateException

Prev Tutorial Next Tutorial

Google Advertisements
Download Projects

Google Adsense Advertisements

Buy 3 Year Old Domain, Tutorialday.com, SeosmoTools.com @$200

Buy This Ad Space @$50 per Month, Ad Size 300X250 Contact on: hitesh.xc@gmail.com WhatsApp no. 8882538826

Yahoo Advertisements