Throw new Exception()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gaya3
    New Member
    • Aug 2007
    • 184

    Throw new Exception()

    Hi,
    In the following example i'm throwing new exception from "try" block.But i havent seen the thrown exception anywhere while running the program.Eventho ugh there is "Finally" block ,the exception should be thrown and "finally" block should be executed.Here it was not so,because the exception is not thrown.can anyone please help me. please correct me if i'm wrong.

    Code:
    public class Test3{
        public static void main(String args[]){
            System.out.println(method());
        }
        public static int method(){
            try{
                throw new Exception();
            }
            catch(Exception e){
                throw new Exception();
            }
            finally{
                return 3;
            }
        }
    }
    -Thanks & Regards,
    Hamsa
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Originally posted by gaya3
    Hi,
    In the following example i'm throwing new exception from "try" block.But i havent seen the thrown exception anywhere while running the program.Eventho ugh there is "Finally" block ,the exception should be thrown and "finally" block should be executed.Here it was not so,because the exception is not thrown.can anyone please help me. please correct me if i'm wrong.

    Code:
    public class Test3{
        public static void main(String args[]){
            System.out.println(method());
        }
        public static int method(){
            try{
                throw new Exception();
            }
            catch(Exception e){
                throw new Exception();
            }
            finally{
                return 3;
            }
        }
    }
    -Thanks & Regards,
    Hamsa
    There's something strange about your catch block there... So tell me, what exactly happenes when a Exception is thrown?
    Greetings,
    Nepomuk

    Comment

    • talonx
      New Member
      • Mar 2008
      • 18

      #3
      Originally posted by gaya3
      Hi,
      In the following example i'm throwing new exception from "try" block.But i havent seen the thrown exception anywhere while running the program.Eventho ugh there is "Finally" block ,the exception should be thrown and "finally" block should be executed.Here it was not so,because the exception is not thrown.can anyone please help me. please correct me if i'm wrong.

      Code:
      public class Test3{
          public static void main(String args[]){
              System.out.println(method());
          }
          public static int method(){
              try{
                  throw new Exception();
              }
              catch(Exception e){
                  throw new Exception();
              }
              finally{
                  return 3;
              }
          }
      }
      -Thanks & Regards,
      Hamsa
      You have committed a very common mistake that beginners do - returning from a finally block. Since its the last thing to be executed before it returns from the method, the exception thrown , or anything else returned, is lost.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        I can't help it but that catch block reminds me of a short stop in a baseball game:
        catch the ball and throw it away as fast as possible again.

        kind regards,

        Jos ;-)

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          Originally posted by JosAH
          I can't help it but that catch block reminds me of a short stop in a baseball game:
          catch the ball and throw it away as fast as possible again.

          kind regards,

          Jos ;-)
          Actually, it's like catching a ball and throwing a different ball away as fast as possible... ^^

          Greetings,
          Nepomuk

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by nepomuk
            Actually, it's like catching a ball and throwing a different ball away as fast as possible... ^^

            Greetings,
            Nepomuk
            Yep, but in this particular example a 'finally block' comes in and returns 3
            before the other party can catch my new ball.

            kind regards,

            Jos

            Comment

            Working...