find the output...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • outofmymind
    New Member
    • Oct 2006
    • 45

    find the output...

    Hi,

    I'd just like to ask if anyone would be able to help me find the out put of this peice of code...i took it from a past exam paper,,,,,and i was wondering wat the output maybe...

    Code:
    public class UsingExceptions {
       // execute application
       public static void main( String args[] )
       {  // call method1 
          try {
             method1();
          }
          // catch Exceptions thrown from method1
          catch ( Exception exception ) {
          System.err.println( exception.getMessage() + "\n" );
             exception.printStackTrace();
          }
       }
       // call method2; throw exceptions back to main
       public static void method1() throws Exception
       {
          method2();
       }
       // call method3; throw exceptions back to method1
       public static void method2() throws Exception
       {
          method3();
       }
       // throw Exception back to method2
       public static void method3() throws Exception
       {
          throw new Exception( "Exception thrown in method3" );
       }
    }  // end class Using Exceptions
    thanks
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by outofmymind
    Hi,

    I'd just like to ask if anyone would be able to help me find the out put of this peice of code...i took it from a past exam paper,,,,,and i was wondering wat the output maybe...

    Code:
    public class UsingExceptions {
    // execute application
    public static void main( String args[] )
    { // call method1 
    try {
    method1();
    }
    // catch Exceptions thrown from method1
    catch ( Exception exception ) {
    System.err.println( exception.getMessage() + "\n" );
    exception.printStackTrace();
    }
    }
    // call method2; throw exceptions back to main
    public static void method1() throws Exception
    {
    method2();
    }
    // call method3; throw exceptions back to method1
    public static void method2() throws Exception
    {
    method3();
    }
    // throw Exception back to method2
    public static void method3() throws Exception
    {
    throw new Exception( "Exception thrown in method3" );
    }
    } // end class Using Exceptions
    thanks
    Suppose you have a go first and then others can make suggestions.

    Comment

    • outofmymind
      New Member
      • Oct 2006
      • 45

      #3
      ok............. ..i compiled it and i got this:


      Exception thrown in method3

      java.lang.Excep tion: Exception thrown in method3
      at UsingExceptions .method3(UsingE xceptions.java: 27)
      at UsingExceptions .method2(UsingE xceptions.java: 22)
      at UsingExceptions .method1(UsingE xceptions.java: 17)
      at UsingExceptions .main(UsingExce ptions.java:6)

      so i just want to know what exactly is the exception...... .im not good at readiing them. And im suspecting that if i got this que in my exam i'd only have to say that the output is: Exception thrown in method3
      wont I ??

      thanks

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by outofmymind
        ok............. ..i compiled it and i got this:


        Exception thrown in method3

        java.lang.Excep tion: Exception thrown in method3
        at UsingExceptions .method3(UsingE xceptions.java: 27)
        at UsingExceptions .method2(UsingE xceptions.java: 22)
        at UsingExceptions .method1(UsingE xceptions.java: 17)
        at UsingExceptions .main(UsingExce ptions.java:6)

        so i just want to know what exactly is the exception...... .im not good at readiing them. And im suspecting that if i got this que in my exam i'd only have to say that the output is: Exception thrown in method3
        wont I ??

        thanks
        When an exception is thrown, e.printStackTra ce() is called which traces the paths that led to the exception.
        Your solution might or might not be correct depending on whether the stackTrace is being considered part of the program's output or not.

        Comment

        Working...