Pages

Showing posts with label try. Show all posts
Showing posts with label try. Show all posts

Sunday, September 15, 2013

Java Interview questions on exceptional handling

1.)What happens if main() is written with out String args[]?
--The code compiles but JVM cannot run it,as it cannot see the main() with String args[]

2.)What are checked exceptions?
--Exceptions that are checked by compiler at the compilation time are called as checked exceptions.

3.)What are unchecked exceptions?
--Exceptions that are checked by JVM are called as unchecked exceptions.

4.)What is Throwable?
--Throwable is a class that represents  all errors and exceptions that occur in java

5.)What is the superclass for all exceptions?
--Exception is the superclass for all the exceptions.

6)What is the difference between exception and an error?
--Exception is a situation which can be handled using try catch
--Error cannot be handled by the programmer and leads to abnormal program termination.
    Ex: OutOfMemory 

7)Difference between throws and throw?
--throws clause is used when the programmer does not want to handle the exception and throw it out of method.
--throw clause is used when the programmer wants to throw an exception explicitly and wants to handle it using catch block.

8)Can we rethrow an exception??
--Yes ,we can rethrow an exception from catch block to another class where it can be handled

Points on exceptional handling

  •  Exceptions  can be handled using try and catch blocks.
  • A try block can be followed with any number of catch blocks.
  • Statements are not allowed in between try and catch blocks
  • It is possible to handle multiple exceptions using  multiple catch blocks
  • Catch block cannot  exist independently ..it should be preceded with a  try block whereas try block need not be followed compulsorily by catch block ,a finally block may follow it.
  •  Nested try blocks are supported in java