Printing the stacktrace

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pjerald
    New Member
    • Oct 2007
    • 77

    #1

    Printing the stacktrace

    I get the code from our articles for exception.
    I just modified the code and tried to run it.
    But it scold me..as follows.

    MyException.jav a:44: 'void' type not allowed here
    System.err.prin tln("test : "+e.printStackT race());
    ^
    1 error



    Kindly read the program...

    Code:
    package test;
    
    public class MyException
    {
    public void calculate(double a, double b, char func) throws Exception
    {
    	double result;
    	switch(func)
    	{
    		case '+' : result = add(a,b); break;
    		case '-' : result = sub(a,b); break;
    		case '*' : result = mul(a,b); break;
    		case '/' : result = div(a,b); break;
    		default  : result = 0;
    	}
    	System.out.println(a + " " + func + " " + b + " = " + result);
    }
    public double add(double a, double b)
    {
    	return a+b;
    }
    public double sub(double a, double b)
    {
    	return a-b;
    }
    public double mul(double a, double b)
    {
    	return a*b;
    }
    public double div(double a, double b) throws Exception
    {
    	if (b==0) throw new Exception();
    	return a/b;
    }
    public static void main(String args[])
    {
    	MyException obj = new MyException();
    	try
    	{
    	obj.calculate(2.3,3.4,'+');
    	}
    	catch(Exception e)
    	{
    		System.err.println("test : "+e.printStackTrace());
    	}
    }
    }

    Awaiting for response,
    Pjerald
  • pjerald
    New Member
    • Oct 2007
    • 77

    #2
    What is wrong with me...

    I get the code from our articles for exception.
    I just modified the code and tried to run it.
    But it scold me..as follows.

    MyException.jav a:44: 'void' type not allowed here
    System.err.prin tln("test : "+e.printStackT race());
    ^
    1 error



    Kindly read the program...

    Code:
    package test;
    
    public class MyException
    {
    public void calculate(double a, double b, char func) throws Exception
    {
    	double result;
    	switch(func)
    	{
    		case '+' : result = add(a,b); break;
    		case '-' : result = sub(a,b); break;
    		case '*' : result = mul(a,b); break;
    		case '/' : result = div(a,b); break;
    		default  : result = 0;
    	}
    	System.out.println(a + " " + func + " " + b + " = " + result);
    }
    public double add(double a, double b)
    {
    	return a+b;
    }
    public double sub(double a, double b)
    {
    	return a-b;
    }
    public double mul(double a, double b)
    {
    	return a*b;
    }
    public double div(double a, double b) throws Exception
    {
    	if (b==0) throw new Exception();
    	return a/b;
    }
    public static void main(String args[])
    {
    	MyException obj = new MyException();
    	try
    	{
    	obj.calculate(2.3,3.4,'+');
    	}
    	catch(Exception e)
    	{
    		System.err.println("test : "+e.printStackTrace());
    	}
    }
    }

    Awaiting for response,
    Pjerald

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      The printStackTrace method doesn't return anything (void) so it can not be used
      in an expression. Just use it as a statement.

      kind regards,

      Jos

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        e.printStackTra ce() is void but you are trying to use it as a right operand for the + operator.
        Open the API docs for Exception and you'll see all the methods that return Strings. Just pick one that you want.

        Comment

        • pjerald
          New Member
          • Oct 2007
          • 77

          #5
          AAhhaa I got it. Thank you jos.
          But still i dont understand why it does not work first.
          I just enclosed e.printStackTra ce() in double quotes,

          Apology.

          Sorry for the double posting.


          Thanks and Regards,
          P.Jerald

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by pjerald
            AAhhaa I got it. Thank you jos.
            But still i dont understand why it does not work first.
            I just enclosed e.printStackTra ce() in double quotes,

            Apology.

            Sorry for the double posting.


            Thanks and Regards,
            P.Jerald
            I've merged the threads.
            Read the replies again to see why it didn't work at first.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by pjerald
              AAhhaa I got it. Thank you jos.
              But still i dont understand why it does not work first.
              I just enclosed e.printStackTra ce() in double quotes,
              I don't understand that remark: if you put something between double quotes
              you're effectively turning it into a String which can be printed.

              kind regards,

              Jos

              Comment

              • pjerald
                New Member
                • Oct 2007
                • 77

                #8
                Originally posted by JosAH
                I don't understand that remark: if you put something between double quotes
                you're effectively turning it into a String which can be printed.

                kind regards,

                Jos
                Ya I know. But if i not put a try catch it scolds me like this,


                MyException.jav a:38: unreported exception java.lang.Excep tion; must be caught or declared to be thrown
                obj.calculate(2 .3,3.4,'+');
                ^
                1 error
                2.3 + 3.4 = 5.6999999999999 99


                For this reason i put a try and catch.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by pjerald
                  Ya I know. But if i not put a try catch it scolds me like this,


                  MyException.jav a:38: unreported exception java.lang.Excep tion; must be caught or declared to be thrown
                  obj.calculate(2 .3,3.4,'+');
                  ^
                  1 error
                  2.3 + 3.4 = 5.6999999999999 99


                  For this reason i put a try and catch.
                  I don't understand what it's all about anymore: first you're talking about putting
                  things in double quotes and now you're talking about something completely
                  different. Your div method throws an Exception so your calculate method throws
                  an exception so you must either handle it or declare that you throw it yourself.

                  kind regards,

                  Jos

                  Comment

                  • pjerald
                    New Member
                    • Oct 2007
                    • 77

                    #10
                    Originally posted by pjerald
                    Ya I know. But if i not put a try catch it scolds me like this,


                    MyException.jav a:38: unreported exception java.lang.Excep tion; must be caught or declared to be thrown
                    obj.calculate(2 .3,3.4,'+');
                    ^
                    1 error
                    2.3 + 3.4 = 5.6999999999999 99


                    For this reason i put a try and catch.
                    Yes i got it. Thank you friends.
                    Thanks,
                    P.jerald

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by pjerald
                      Originally posted by pjerald
                      Ya I know. But if i not put a try catch it scolds me like this,


                      MyException.jav a:38: unreported exception java.lang.Excep tion; must be caught or declared to be thrown
                      obj.calculate(2 .3,3.4,'+');
                      ^
                      1 error
                      2.3 + 3.4 = 5.6999999999999 99

                      For this reason i put a try and catch.
                      Yes i got it. Thank you friends.
                      Thanks,
                      P.jerald
                      You do realize that you're talking to yourself now don't you? ;-)

                      kind regards,

                      Jos

                      Comment

                      • pjerald
                        New Member
                        • Oct 2007
                        • 77

                        #12
                        Originally posted by JosAH
                        You do realize that you're talking to yourself now don't you? ;-)

                        kind regards,

                        Jos
                        mmmm your are right.
                        All because of my poor knowledge in java.

                        I wish to be an expert like you in java.


                        Thanks,
                        P.Jerald

                        Comment

                        • pjerald
                          New Member
                          • Oct 2007
                          • 77

                          #13
                          Originally posted by pjerald
                          mmmm your are right.
                          All because of my poor knowledge in java.

                          I wish to be an expert like you in java.


                          Thanks,
                          P.Jerald


                          Now see, I ran my program And it printed an exception as follows.

                          I used printStackTrace () in y program( The same above one.).


                          javac MyException.jav a -d .; java test.MyExceptio n 0
                          java.lang.Excep tion
                          at test.MyExceptio n.div(MyExcepti on.java:32)
                          at test.MyExceptio n.calculate(MyE xception.java:1 3)
                          at test.MyExceptio n.main(MyExcept ion.java:39)



                          What is the div specified here. my package name is test.

                          Comment

                          • pjerald
                            New Member
                            • Oct 2007
                            • 77

                            #14
                            I added a print statement after the catch part.

                            And i ran the program to cause an exception. Using division by zero.

                            It gave me an exception and also it printed the test print statement.

                            But i studied theoretically that if an exception caused, the jvm just throw an exception and exit the program. And how my print statement executed ?


                            See this,
                            javac MyException.jav a -d .; java test.MyExceptio n 0
                            java.lang.Excep tion
                            at test.MyExceptio n.div(MyExcepti on.java:32)
                            at test.MyExceptio n.calculate(MyE xception.java:1 3)
                            at test.MyExceptio n.main(MyExcept ion.java:39)
                            test :

                            test is my print statement.

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by pjerald
                              I added a print statement after the catch part.

                              And i ran the program to cause an exception. Using division by zero.

                              It gave me an exception and also it printed the test print statement.

                              But i studied theoretically that if an exception caused, the jvm just throw an exception and exit the program. And how my print statement executed ?


                              See this,
                              javac MyException.jav a -d .; java test.MyExceptio n 0
                              java.lang.Excep tion
                              at test.MyExceptio n.div(MyExcepti on.java:32)
                              at test.MyExceptio n.calculate(MyE xception.java:1 3)
                              at test.MyExceptio n.main(MyExcept ion.java:39)
                              test :

                              test is my print statement.
                              Perhaps it's time you have a look at at this?

                              Comment

                              Working...