Saturday, April 6, 2019

Exception handling in java

Q- Exception Hierarchy In Java ?



Q- What are the different types of exceptions in Java?

There are mainly two types of exceptions in java: checked exception and unchecked exception. .

Errors are the conditions which cannot get recovered by exception handling.
  • Arithmetic Exception: This exception thrown in an arithmetic operation. When an exceptional condition has occurred.
  • ArrayIndexOutOfBoundException: This exception thrown to indicate that an array has been accessed or fill data in array with an illegal index. The index is either negative or greater than or equal to the size of the array.
  • ClassNotFoundException: This exception thrown when we try to access a class whose definition is not found.
  • FileNotFoundException: This exception thrown when a file is not accessible or does not exist as given location and unable open file.
  • IOException: It is thrown when an input-output operation failed or interrupted
  • InterruptedException:Tthis exception is thrown when a thread is doing some processing or waiting or sleeping and it is interrupted.
  • NoSuchFieldException: This exception is thrown when a class does not contain the field (or variable) specified
  • NoSuchMethodException: This exception is thrown when accessing a method which is not found means call wrong method name which is not exist.
  • NullPointerException: This exception is thrown when referring to the members of a null object. Null represents nothing or call any method on reference which has null vale.
  • NumberFormatException: This exception is raised when a method could not convert a string into a numeric format.
  • StringIndexOutOfBoundsException: This exception is thrown by String class methods to indicate that an index is either negative than the size of the string.
  • IllegalArgumentException: It is RuntimeException, this exception is thrown while a method is called with incorrect input arguments. Then, the only thing we must do is correct the values of the input parameters
  • UnsupportedOperationException: It is RuntimeException, this exception is Thrown to indicate that the requested operation is not supported.
  •  
Q- Exception propagation in java. How checked and unchecked exceptions are propagated?

Q- How many ways to handle exception in java
Answer:- 
  • try catch block
  • try-with-resources :- This was introduce in java 7
  • Custom exception handling
  • Global exception handling
Q- throw vs throws vs throwable
Answer:-
  • throw  :- throw is a keyword in java which is used to throw an exception.
  • throws :- throws is used with the method signature.
  • throwable :- Throwable class is the superclass of all errors and exceptions in the Java language.
Q- StackOverflowError Vs OutOfMemoryError In Java ?

Related Tutorial

No comments:

Post a Comment