Answered Questions

  • difference between throw and throws

    What exactly is the difference between between throw and throws? If both are used for the same purpose then why are both needed instead of one?

    Star Read Best Answer

    Editorial / Best Answer

    vegetto  

    • Member Since Jun-2008 | Jun 15th, 2008


    hi there,
                 
    throw:the throw keyword is used to throw the exception manually,that is when you feel a particular line in your code, when executed is capable of having some exception then you use throw keyword as:
     
    throw new MyException(arguments if any);
    this leads to instantiating of the MyException class and exception is thrown


    throws: this is to be used when you are not using the try catch statement in your code but you know that this particular class is capable of throwing so and so exception(only checked exceptions).in this you do not use try catch block but write using the throw clause at appropriate point in  you code and the exception is thrown to caller of the method and is handeled by it. eg:

    void trouble()throws IOException,any other exceptions just separate them with commas.......
    {
       /* throw statement at appropriate step,
        no need to use try catch here,exception would be thrown to caller and u should    provide try catch block to handle exception there else this process cotinues further till
       appropriately it is handeled or prog terminates abruptly */
    }


    this is called as HANDLE OR DECLARE RULE  i.e either u handle exceptions using try catch block or by declaring them
     

     

  • How JPA and hibernate are related as per EJB 3 specifications?

    Sri

    • Oct 12th, 2007

    JPA is official acceptance from SUN about its failure on EJB. It has to abandon its EJB model to go to ORM model. Finally, Sun  should provide developers some tools to migrate Java programs to .net easily with out pain

  • What is the difference between an object and an instance? And give me a real time example to differentiate these two?

    Sarje

    • Aug 15th, 2009

    object and instance both are sameI think you want know the difference between reference and object.If so thenreference of any class type is a variable that can hold the reference(more or less we can s...

    pravinsharma

    • Oct 15th, 2008

    An object is part of a process/thread which demands processor time through the JVM.The act of creating the object from a class is called instantiation.In literary sense an instance is existance/occurrence of an object.In this case a duplicate copy(instance) of an object is still the same object.