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... -
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
15 3640
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... -
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
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
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.
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
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.
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
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.
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
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
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
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
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.
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.
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?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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.
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
| |