473,327 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Printing the stacktrace

77
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.java:44: 'void' type not allowed here
System.err.println("test : "+e.printStackTrace());
^
1 error



Kindly read the program...

Expand|Select|Wrap|Line Numbers
  1. package test;
  2.  
  3. public class MyException
  4. {
  5. public void calculate(double a, double b, char func) throws Exception
  6. {
  7.     double result;
  8.     switch(func)
  9.     {
  10.         case '+' : result = add(a,b); break;
  11.         case '-' : result = sub(a,b); break;
  12.         case '*' : result = mul(a,b); break;
  13.         case '/' : result = div(a,b); break;
  14.         default  : result = 0;
  15.     }
  16.     System.out.println(a + " " + func + " " + b + " = " + result);
  17. }
  18. public double add(double a, double b)
  19. {
  20.     return a+b;
  21. }
  22. public double sub(double a, double b)
  23. {
  24.     return a-b;
  25. }
  26. public double mul(double a, double b)
  27. {
  28.     return a*b;
  29. }
  30. public double div(double a, double b) throws Exception
  31. {
  32.     if (b==0) throw new Exception();
  33.     return a/b;
  34. }
  35. public static void main(String args[])
  36. {
  37.     MyException obj = new MyException();
  38.     try
  39.     {
  40.     obj.calculate(2.3,3.4,'+');
  41.     }
  42.     catch(Exception e)
  43.     {
  44.         System.err.println("test : "+e.printStackTrace());
  45.     }
  46. }
  47. }
  48.  

Awaiting for response,
Pjerald
Oct 18 '07 #1
15 3640
pjerald
77
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.java:44: 'void' type not allowed here
System.err.println("test : "+e.printStackTrace());
^
1 error



Kindly read the program...

Expand|Select|Wrap|Line Numbers
  1. package test;
  2.  
  3. public class MyException
  4. {
  5. public void calculate(double a, double b, char func) throws Exception
  6. {
  7.     double result;
  8.     switch(func)
  9.     {
  10.         case '+' : result = add(a,b); break;
  11.         case '-' : result = sub(a,b); break;
  12.         case '*' : result = mul(a,b); break;
  13.         case '/' : result = div(a,b); break;
  14.         default  : result = 0;
  15.     }
  16.     System.out.println(a + " " + func + " " + b + " = " + result);
  17. }
  18. public double add(double a, double b)
  19. {
  20.     return a+b;
  21. }
  22. public double sub(double a, double b)
  23. {
  24.     return a-b;
  25. }
  26. public double mul(double a, double b)
  27. {
  28.     return a*b;
  29. }
  30. public double div(double a, double b) throws Exception
  31. {
  32.     if (b==0) throw new Exception();
  33.     return a/b;
  34. }
  35. public static void main(String args[])
  36. {
  37.     MyException obj = new MyException();
  38.     try
  39.     {
  40.     obj.calculate(2.3,3.4,'+');
  41.     }
  42.     catch(Exception e)
  43.     {
  44.         System.err.println("test : "+e.printStackTrace());
  45.     }
  46. }
  47. }
  48.  

Awaiting for response,
Pjerald
Oct 18 '07 #2
JosAH
11,448 Expert 8TB
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
Oct 18 '07 #3
r035198x
13,262 8TB
e.printStackTrace() 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.
Oct 18 '07 #4
pjerald
77
AAhhaa I got it. Thank you jos.
But still i dont understand why it does not work first.
I just enclosed e.printStackTrace() in double quotes,

Apology.

Sorry for the double posting.


Thanks and Regards,
P.Jerald
Oct 18 '07 #5
r035198x
13,262 8TB
AAhhaa I got it. Thank you jos.
But still i dont understand why it does not work first.
I just enclosed e.printStackTrace() 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.
Oct 18 '07 #6
JosAH
11,448 Expert 8TB
AAhhaa I got it. Thank you jos.
But still i dont understand why it does not work first.
I just enclosed e.printStackTrace() 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
Oct 18 '07 #7
pjerald
77
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.java:38: unreported exception java.lang.Exception; must be caught or declared to be thrown
obj.calculate(2.3,3.4,'+');
^
1 error
2.3 + 3.4 = 5.699999999999999


For this reason i put a try and catch.
Oct 18 '07 #8
JosAH
11,448 Expert 8TB
Ya I know. But if i not put a try catch it scolds me like this,


MyException.java:38: unreported exception java.lang.Exception; must be caught or declared to be thrown
obj.calculate(2.3,3.4,'+');
^
1 error
2.3 + 3.4 = 5.699999999999999


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
Oct 18 '07 #9
pjerald
77
Ya I know. But if i not put a try catch it scolds me like this,


MyException.java:38: unreported exception java.lang.Exception; must be caught or declared to be thrown
obj.calculate(2.3,3.4,'+');
^
1 error
2.3 + 3.4 = 5.699999999999999


For this reason i put a try and catch.
Yes i got it. Thank you friends.
Thanks,
P.jerald
Oct 18 '07 #10
JosAH
11,448 Expert 8TB
Ya I know. But if i not put a try catch it scolds me like this,


MyException.java:38: unreported exception java.lang.Exception; must be caught or declared to be thrown
obj.calculate(2.3,3.4,'+');
^
1 error
2.3 + 3.4 = 5.699999999999999

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
Oct 18 '07 #11
pjerald
77
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
Oct 18 '07 #12
pjerald
77
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.java -d .; java test.MyException 0
java.lang.Exception
at test.MyException.div(MyException.java:32)
at test.MyException.calculate(MyException.java:13)
at test.MyException.main(MyException.java:39)



What is the div specified here. my package name is test.
Oct 18 '07 #13
pjerald
77
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.java -d .; java test.MyException 0
java.lang.Exception
at test.MyException.div(MyException.java:32)
at test.MyException.calculate(MyException.java:13)
at test.MyException.main(MyException.java:39)
test :

test is my print statement.
Oct 18 '07 #14
r035198x
13,262 8TB
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.java -d .; java test.MyException 0
java.lang.Exception
at test.MyException.div(MyException.java:32)
at test.MyException.calculate(MyException.java:13)
at test.MyException.main(MyException.java:39)
test :

test is my print statement.
Perhaps it's time you have a look at at this?
Oct 18 '07 #15
jkmyoung
2,057 Expert 2GB
e.printStackTrace();
prints to default to System.err by default.
You could use e.printStackTrace(System.err); if you really wanted.

I'm assuming you changed your output to the following?
e.printStackTrace();
System.err.println("test:");

If so, just reverse those 2 lines.
Oct 18 '07 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Will Ware | last post by:
I was fooling with some Python code, and starting to miss the Exception.printStackTrace() feature in Java. Here is a stab at something roughly analogous, which puts together a stacktrace as an XML...
0
by: MTK | last post by:
My application hangs at times when creating an instance of the Systems.Diagnostics.Stacktrace. StackTrace st = new StackTrace(true); This doesnt happen all the time, just once in many days. ...
4
by: Barry Mossman | last post by:
Hi, I am throwing an exception derived from ApplicationException as follows catch (Exception ex) { throw new MyException("message", ex); } My exception's constructor is: public...
3
by: Kurt Biesemans | last post by:
Hello, I have a line code : StackTrace stackTrace = new StackTrace(false); When I run the project in debug, the application keeps hanging on this line. My Environment: Windows 2003 Server...
0
by: coolsilver.net | last post by:
I am not sure if I am in the right group. My problem is this: My VB.NET application prints to the Epson printer using Epson OPOS drivers. I have downloaded the Epson ADK for .NET from the...
3
by: Mark R. Dawson | last post by:
Hi all, I am trying to get custom attributes from a property. I can do this if I pass in the name of the property i.e. "Name" to the reflection methods, but if I pass in set_Name which is what...
1
by: Bernard Goh | last post by:
Hi All, I am newbie for ASP.NET programming and I am having this error when I tried to print a crytal report from my dotnet web application. Error Message : Invalid file name. at...
8
by: Rick Lederman | last post by:
I am using a PrintDocument and PrintDialog to print. The first time that I print it works, but when I try to print a second time without exiting the entire program I get an...
1
by: ernesto | last post by:
Hi everybody: I'm writing a log class that uses the StackTrace information in order to write in the log files the name of the classes and methods that produce every log entry. Anyway, doing...
1
by: lesani | last post by:
hi, i want to find out stacktrace of a thread that is only have its thread id (which have created in vc++ project), i can find processthread by searching in ids of...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.