Top 150+ Java Interview Questions and Answers

    58 Votes

100. What are the uses of exceptions?

  • Consistent error reporting style.
  • Easy to pass errors up the stack.
  • Pinpoint errors better in stack trace.
  • As long as you “fail fast” and throw as soon as you find a problem.
  • Wrap useful information on your exception classes and use that information later, while handling the exception.
  • Exceptions don’t always have to be errors, maybe your program can cope with the exception and keep going.
  • Separating error handling code from Regular code

 

101. What are the types of error?
There are two types of error:
  • Compile time error
  • Run time error
102. List some exception occurence reason?
Running out of memory, Resource allocation errors, Inability to find files, Problem in network connectivity, etc.

 

103. What are checked exceptions?

Checked exceptions are the type exceptions which are checked during compilation. A method which throws a checked exception, should advertise the exception in it's method definition. The method which calls another method that advertises a checked exception can handle it in two ways. One option is to handle the thrown exception in a try and catch block or it should advertise the exception thrown by calling method.

104. What are unchecked exceptions?

Exceptions raised implicitly by the system due to illegal code execution are called unchecked exceptions. We don't want to announce the possibility of exception occurrence. When the unchecked exception occurs, and the program is not written to handle it, it would be processed by the default exception handler.

105. How exception handling is implemented using try and catch statement?

The try block encloses a piece of code that may create an exception. It also acts as the scope of the associated catch block which handles exceptions.

106. What is throwing an exception?

The throw statement causes the termination of the normal flow of control of the Java code and stops the execution of the subsequent statements if an exception is thrown.

The throw clause transfers the control to the nearest catch block handling the type of exception object throws. The following syntax shows how to declare the throw statement:

public double divide(int dividend, int divisor) throws ArithmeticException {
if(divisor == 0) {
throw new ArithmeticException(“Divide by 0 error”);
}
return dividend / divisor;
}

107. When should we use throws exceptions?

The throws statement is used by a method to specify the types of exceptions the method throws. If a method is capable of raising an exception that it can't handle, the method must specify that the exception has to be handled by the calling method. This is done using the throws statement.

Example: void readData() throws IOException

108. How can we create our own exceptions?

Defining our own exceptions helps us handle specific exceptions that are tailor-made for our application. Steps to Create a User Defined Exception are:

Create a class that extend from a right kind of class from the Exception Hierarchy. For Example,

public class DivideByZeroException extends ArithmeticException 
{
public DivideByZeroException()
{
super("Divide by 0 error");
}
}

109. What are the exception occurrence levels?

Hardware/operating system level :

  • Arithmetic exceptions; divide by 0, under/overflow.
  • Memory access violations; segment fault, stack over/underflow.

Language level :

  • Type conversion; illegal values, improper casts.
  • Bounds violations; illegal array indices.
  • Bad references; null pointers.

Program level :

  • User defined exceptions.

110. What are the categories of exceptions?

There are two categories of exceptions:

  • Built in Exceptions
    • Checked Exceptions
    • Unchecked Exceptions
  • User Defined Exceptions

111. What is the use of finally clause?

The finally block is used to process certain statements, no matter whether an exception is raised or not. Generally, we write resource cleaning operations inside finally block. Like, closing a Java Inputstream.

Page 10 of 14

Related Articles

Java OOPS Interview Questions and Answers
Top Advanced Java Interview Questions and Answers
Java Collections Interview Questions and Answers
Java Thread Interview Questions and Answers

Popular Videos

How to speak to people

How to speak so that people want to listen.

Got a tip or Question?
Let us know