05 November 2020

#Exception handling

Exception handling
What is an Exception in Java?
What are the Exception Handling Keywords in Java?
Explain Java Exception Hierarchy?
What are the important methods of Java Exception Class?
Explain Java 7 ARM Feature and multi-catch block?
What is the difference between Checked and Unchecked Exceptions in Java?
What is the difference between the throw and throws keyword in Java?
How to write custom exceptions in Java?
What are different scenarios causing “Exception in thread main”?
What is the difference between final, finally, and finalize in Java?
What happens when an exception is thrown by the main method?
Can we have an empty catch block?
What is NullPointerException?
What is ArrayIndexOutOfBoundsException?
What is NumberFormatException?
How the exceptions are handled in java? OR Explain exception handling mechanism in java?
What is try block?
What is catch block?
What is finally block?
What is an exception?
What is the difference between error and exception in java?
What is unreachable catch block error?
What are run time exceptions in java. Give example?
What is OutOfMemoryError in java?
what are checked and unchecked exceptions in java?
What is the difference between ClassNotFoundException and NoClassDefFoundError in java?
What is Re-throwing an exception in java?
What is the use of throws keyword in java?
What is the difference between final, finally and finalize in java?
What is ClassCastException in java?
What is the difference between throw, throws and throwable in java?
What is StackOverflowError in java?
What are chained exceptions in java?
What are the legal combinations of try, catch and finally blocks?
What is the use of printStackTrace() method?
What is ClassNotFoundException?
What is SQLException?
What is IOException
Explain the hierarchy of exceptions in java?
Why it is always recommended that clean up operations like closing the DB resources to keep inside a finally block?
Which class is the super class for all types of errors and exceptions in java?
How the exceptions are handled in java? OR Explain exception handling mechanism in java?
How do you create customized exceptions in java?
Can we keep other statements in between try, catch and finally blocks?
Can we write only try block without catch and finally blocks?
Can we keep the statements after finally block If the control is returning from the finally block itself?
Can we throw an exception manually? If yes, how?
Can we override a super class method which is throwing an unchecked exception with checked exception in the sub class?
Does finally block get executed If either try or catch blocks are returning the control?
Give some examples to checked exceptions?
Give some examples to unchecked exceptions?
  • It is one of the powerful mechanism to handle the runtime errors
  • The java.lang.Throwable class is the root class of Java Exception hierarchy which is inherited by two subclasses: Exception and Error. 
Types
  • Checked Exception
    • The classes which directly inherit Throwable class except RuntimeException and Error are known as checked exceptions 
    • e.g. IOException, SQLException etc. Checked exceptions are checked at compile-time.
  • Unchecked Exception
    • The classes which inherit RuntimeException are known as unchecked exceptions 
    • e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. 
    • Unchecked exceptions are not checked at compile-time, but they are checked at runtime.
  • Error - It is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
  • try - It is used to specify a block where we should place exception code. The try block must be followed by either catch or finally. It means, we can't use try block alone.
  • catch - It is used to handle the exception. It must be preceded by try block which means we can't use catch block alone. It can be followed by finally block later.
  • finally - It is used to execute the important code of the program. It is executed whether an exception is handled or not.
  • throw - It is used to throw an exception.
  • throws - It is used to declare exceptions. It doesn't throw an exception. It specifies that there may occur an exception in the method. It is always used with method signature.
Example
  • ArithmeticException - It is thrown when an exceptional condition has occurred in an arithmetic operation.
  • ArrayIndexOutOfBoundsException - It is thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.
  • ClassNotFoundException - This Exception is raised when we try to access a class whose definition is not found
  • FileNotFoundException - This Exception is raised when a file is not accessible or does not open.
  • IOException - It is thrown when an input-output operation failed or interrupted
  • InterruptedException - It is thrown when a thread is waiting , sleeping , or doing some processing , and it is interrupted.
  • NoSuchFieldException - It is thrown when a class does not contain the field (or variable) specified
  • NoSuchMethodException - It is thrown when accessing a method which is not found.
  • NullPointerException - This exception is raised when referring to the members of a null object. Null represents nothing
  • NumberFormatException - This exception is raised when a method could not convert a string into a numeric format.
  • RuntimeException- This represents any exception which occurs during runtime.
  • StringIndexOutOfBoundsException - It is thrown by String class methods to indicate that an index is either negative than the size of the string

No comments:

Post a Comment

Most views on this month