Exception handling in java interview questions in java
- What is Exception.
2. What is a purpose of exception.
ans. The purpose of an exception handling is graceful termination of our program.
3.What is default natural exception handling.
ans. In which method exception rises ,this method is responsible to create the object of exception of and handover the jvm. Then jvm come across the method .if the method dose not contain handling code of exception then jvm terminates the method abnormally and check the caller method .if caller method also dose not contain the handling code then jvm terminates the caller method also.This process is going on until main method.if main method also does not contain handling code then jvm terminates the main method also.After then jvm call default exception handler and default exception handler is responsible to print the message on the console and terminates abnormally.
4.What is a root class of an exception handling in java
ans. Throwable.
5.what is checked exception.
ans. The exceptions which are checking by the compiler because our program execution will become easy .such type of exceptions are known as checked exceptions.
ex:FileNotFoundException,InterruptedException etc
6.What is unchecked exception.
ans.Exceptions which are not checking by the compiler .whether the programmer handle 's exception code or not.such type of exceptions are known as unchecked exceptions.
ex:ArithmeticException, NullPointerException,ClassCastException etc.
7.What is difference between Exception and Error.
ans. Exceptions rises by our programming code.programmer is responsible to generated the exceptions. Exceptions are recoverable.
Errors are not handling event.we can not handle the errors explicitly .suppose OutOfMemoryError generates,being a programmer we can't to anything.
8.What is fully checked or partially checked exceptions.
ans.If The checked exception and it's child classes are also checked such type of exceptions are known as fully checked exceptions.like Interrupted exception, Io exceptions.
if Checked exception and it's child classes are unchecked .such type of exceptions are known as partially checked.
9.What is the main purpose of throw keyword in java.
ans. Sometimes we create exception object explicitly and handover the exception object to jvm manually by using throw keyword.
ex: class Demo
{
public static void main(String[] args)
{
System.out.println(10/0);
}
}
output.
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Demo.main(Demo22.java:5)
ex.2
class Demo
{
public static void main(String[] args)
{
throw new ArirhmeticException("/ by zero");
}
}
output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Demo.main(Demo22.java:5)
The output is same in given ex:1 or ex:2 but the difference is in ex:1 default exception handler is
responsible to create exception object implicitly .
bu in ex:2 we are creating an exception object explicitly and handover to the JVM manually.
10.What is throws keyword in java.
ans.If there may be a chance to create checked exception in our program .then we should handle that exception either by try-catch or throws keyword.other wise we will get compile time error.
ex:1
class Demo
{
public static void main(String[] args)
{
Thread.sleep(2000);
}
}
output:
compile time error:unreported exception InterruptedException; must be caught or declared to be thrown
Thread.sleep(2000);
ex:2
import java.io.*;
class Demo
{
public static void main(String[] args)
{
PrintWriter pw=new PrintWriter("xyz.txt");
pw.println("hello");
}
}
output:
compile time error:unreported exception FileNotFoundException; must be caught or declared to be thrown
PrintWriter pw=new PrintWriter("xyz.txt");
We can handle this error either by using try-catch or throws keyword.
1.try-catch:
class Demo
{
public static void main(String[] args)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{}
}
}
2.throws keyword:
The main purpose of throws keyword is to daligates the responsibility of exception handling code to the caller(it may be jvm or caller method)
ex:1
class Demo
{
public static void main(String[] args) throws InterruptedException
{
Thread.sleep(2000);
}
}
ex:2
class Demo
{
public static void main(String[] args)throws InterruptedException
{
m1();
}
public static void m1()throws InterruptedException
{
m2();
}
public static void m2()throws InterruptedException
{
Thread.sleep(2000);
}
}
If we remove anyone throws keyword then we will get compile time error.Because m2() occure
checked exception and delegates to the caller.