Search your topic in Java2share
Home > FAQs
eXTReMe Tracker


Exception Handling FAQs


Untitled Document

Q21.  Is syntax try-finally-catch is valid ?
Ans.   No, this syntax is not valid. It should be like try-catch-finally then only code will compile.
Q22. What is the purpose of throw?
Ans.   Sometimes we can create Exception object explicitly and we can handover that exception object to the JVM explicitly by throw keyword.
          The purpose of throw keyword is to handover our created exception object explicitly to the JVM.
                Example1:
class Test{
                public static void main(String[] args){
                                System.out.println(10/0);
               }
                }
                In this case ArithmeticException object created implicitly and handover to the JVM automatically by the main method.

                Example2:
Class Test{
                Public static void main(String[] args){
                                Throw new ArithmeticException(“/by Zero”);
                                }
                }
                In this case creation of an exception object and handover to the JVM explicitly by the programmer.

Q23.  Is it possible to throw an Error?
Ans.   Yes, It is possible to throw any Throwable type including Error.

Q24.  Is it possible to throw any java object?
Ans.   No, we can use throw keyword only for throwable objects otherwise we will get compile time error saying incompatible type.