Вы находитесь на странице: 1из 5

Questions related to Exception

1. What is Exception and why to use?

Ans:
Exception is nothing but a situation where program terminate abnormally.

2. What is Exception Handling?

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

3. What are the types of exception?

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

4. Difference between checked and unchecked exception with example?

Ans:
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?

Ans:
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?

Ans:
Try block contain the Exception generate code.

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

Ans:
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?

Ans:
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?

Ans:

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

Ans:
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?

Ans:
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?

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

13. What is ArrayIndexOutOfBoundExceptipon?

Ans:
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?

Ans:
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?

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

Ans:
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?

Ans:
This exception is thrown when there has been made an attempt to store the wrong type
of object into an array of objects.

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


method which throws RuntimeException?

Ans:
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?

Ans:
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?

Ans:
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?

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

22. When try block execute?

Ans:
Try block compulsory execute when program already executed.

23. What is the use of catch block?

Ans:
Catch block contain exception handle code.

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

Ans:
Multiple catch is allowed but maximum one catch can be execute at a single time.
25. Is execution of catch is optional?

Ans:
Yes execution of catch is purely optional.

26. Define PrintStackTrace?

Ans:
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?

Ans:
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?

Ans:
Maximum one finally allow for a single try.

29. Which Exception throws by overridden method?

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

30. Who is the superclass of Exception?

Ans:
Throwable.

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

Ans:
Exception Handling.

32. What is Exception Propagation?

Ans:
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?


Ans:
There are 2 types of Error present in java.
i) Compile time Error
ii) Run time Error.

34. What is custom Exception?

Ans:
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?

Ans:
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?

Ans:
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.

Вам также может понравиться