Java Interview Questions - Exception

1. What is Exception and why to use?

Exception is nothing but a situation where program terminate abnormally.

2. What is Exception Handling?

It is a technique through which program can be protected from abnormal termination.

3. What are the types of exception?

Exception are of 2 types 1) Checked Exception 2) Unchecked Exception

4. Difference between checked and unchecked exception with example?

Checked Exception - The Exception which generate at the time of compilation & handle at that time is known as Checked Exception. Except Runtime & its child class all other exception are checked exception. The Exception which generate at the time of execution is known as Unchecked Exception. RuntimeException and its child class are treated as unchecked exception.

5. What are the keyword use in case of exception handling?

There are 5 keyword provide by java they are:

  1. try
  2. catch
  3. Throw
  4. Throws
  5. finally

6. What is the use try block?

Try block contain the Exception generate code.

7. What is the difference between catch and finally block in java?

Difference between catch and finally is:

Catch generally contain exception handle code where finally always contain unreachable code & closing of connection. The execution of catch is optional where the execution of finally is compulsory.

8. What is the difference between throw and throws keyword in java?

Diff. b/w throw& throws keyword is:

Throw keyword generate the userdefined or predefined exception explicitly. Where throws keyword transfer the exception of caller of that function.

9. How to create user defined exception in java?

10. What is the rule of exception in case of method overriding?

  1. If the super class method does not declare an exception ,subclass overridden method cannot declare the checked exception but it can declare unchecked exception.
  2. If the super class method declares an exception , subclass overridden method can declare same, sub class exception or no exception but cannot declare parent exception.

11. What is NullPointerException and what is the situation?

If we have null value in the variable ,performing any operation by variable occurs an NullPointerException.

12. What is ArithmeticException and what are the situation?

If we divide any number by zero ,there occurs an ArithmeticException.

13. What is ArrayIndexOutOfBoundExceptipon?

If you are inserting any value in the wrong index , it would result ArrayIndexOutOfBoundExceptipon.

14. How to handle exception by super class & specify the restriction?

Exception is the super class of all exception in java, so this class can handle any type of exception generate by java program. Only restriction is Exception catch block should be last catch block among exception hierarchy.

15. What things should be kept in mind while creating your own exceptions in Java?

16. Does it matter in what order catch statements for FileNotFoundException and IOException are written?

Before handle by super class I,e IOException, handle with sub class FileNotFoundEXception. Otherwise it is an error in java.

17. When is the ArrayStoreException thrown?

18. If a method throws NullPointerException in super class, can we override it with a method which throws RuntimeException?

Yes possible because these are the unchecked exception.

19. Is there any way to skip Finally block of exception even if some exception occurs in the exception block?

If user exit from application by System.exit(0) method.

20. Who is the super class of all the Exception in java & How it handle Exception?

Exception is the superclass of all the exception in java so it can handle any kind of exception generate by the program.

21. When try block should be an error?

Try block should be followed by a catch or finally block other wise an error.

22. When try block execute?

Try block compulsory execute when program already executed.

23. What is the use of catch block?

Catch block contain exception handle code.

24. How many catch should execute at a single time?

Multiple catch is allowed but maximum one catch can be execute at a single time.

25. Is execution of catch is optional?

Yes execution of catch is purely optional.

26. Define PrintStackTrace?

PrintStackTrace is a predefined method present in throwable class which always display name of Exception, why Exception occure, and line no of Exception.

27. When finally block execute?

Finally block is a very powerful block in java which execute when try will execute.

28. How many finally block allow for a single try?

Maximum one finally allow for a single try.

29. Which Exception throws by overridden method?

Overridden method only throws that exception which throws by superclass method.

30. Who is the superclass of Exception?

Throwable.

31. Which concept use to convert runtime error into user friendly message by java programmer?

Exception Handling.

32. What is Exception Propagation?

An Exception is first thrown from the top of the stack & if it is not caught ,it drop down the call stack to the previous method,if not caught there ,the exception not drop down to the previous method and so on they are untile they reach the very bottom of the call stack Which is called Exception propagation.

33. Define types of Error?

There are 2 types of Error present in java.

  1. Compile time Error
  2. Run time Error.

34. What is custom Exception?

If you are creating your own Exception that is known as Custom Exception – user defined exception.

35. State the ways to find the details of Exception?

  1. using an object of java.lang.Exception class.
  2. using public void PrintStack method.
  3. using public ssStringgetMessage method.

36. State the rules for handling exception with overriding method?

  1. If the super class method does not declare an exception ,subclass overridden method cannot declare the checked exception but it can declare unchecked exception.
  2. If the super class method declares an exception , subclass overridden method can declare same, sub class exception or no exception but cannot declare parent exception.