my dog learned polymorphism*
The moose likes Java in General and the fly likes java:280: 'void' type not allowed here Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "java:280: Watch "java:280: New topic
Author

java:280: 'void' type not allowed here

Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Hello,

I am trying here to print and send stackTrace in my email to fix the errors.
I have writtent the following methods


I called this method as follow:

but it throws following exception:

I tried to another place as:

and it throws this exception:


Kindly help me to fix it.

Thanks & best regards
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Before going to the context, I notice you have tried to set a code tag but unsuccessful using [cpde]. To use code tags you can easily select the fragment you have to highlight as code and then click the Code button.

1. The printStackTrace() method does not return a String but is void and hence you cannot send it as a String argument to another method.

2. The second error is because the errorMsg() method throws an Exception which you do not handle. You should handle it in the catch clause again. Or include a throws Exception clause in the method declaration similar to one you have in errorMsg() method.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
John Jai wrote:Before going to the context, I notice you have tried to set a code tag but unsuccessful using [cpde]. To use code tags you can easily select the fragment you have to highlight as code and then click the Code button.


Sorry for that. I have corrected


1. The printStackTrace() method does not return a String but is void and hence you cannot send it as a String argument to another method.

Is there a way to get its output as String object?


2. The second error is because the errorMsg() method throws an Exception which you do not handle. You should handle it in the catch clause again. Or include a throws Exception clause in the method declaration similar to one you have in errorMsg() method.


If I declare similar to my errorMsg() method then this method no need to write.

Is there any other solution?

Thanks for your favorable reply
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Interface Throwable has several printStackTrace() methods. The version without arguments prints the stack trace to System.err and does not allow you to get the stack trace in a string. There is however also a version that takes a PrintWriter, which you can use like this:

It's a bit cumbersome, but it works. There are some libraries that have utility methods to make it easier. For example, using Google Guava you could do:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3426
    
  12
Jesper de Jong wrote:Interface Throwable has several printStackTrace() methods. The version without arguments prints the stack trace to System.err and does not allow you to get the stack trace in a string. There is however also a version that takes a PrintWriter, which you can use like this:


If you are going to do this, you should overload your errorMsg function to take an exception as a parameter and put this code in there, rather than converting the exception to a String every place you call errorMsg.


Joanne
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Thanks all from the bottom of my heart for giving me your precious time.

I am still facing unreported exception java.lang.Exception; must be caught or declared to be thrown. I changed my method like this:


this block reporting exception:


Thanks again & best regards
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3426
    
  12
Farakh khan wrote:this block reporting exception:


Your errorMsg method is defined to throw an Exception. You need to catch that exception or declare that the method that this code is in throws an Exception
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Thanks for your reply. I did like this:

and its working but I lost my objective to get the exception in my email. I mean even if this thrown some exception then this exception will not be passed to method and this will not be mailed to me because when am coding like:

it again throws

thanks again
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3426
    
  12
Farakh khan wrote:its working but I lost my objective to get the exception in my email

The most likely thing in your errorMsg method to throw an exception is SendEmail.send(). So by having a method that throws an exception if it can't send an email and catching that exception and trying to send another email is going to end up in an infinite loop. It would be better to catch any exception thrown by SendEmail.send in your errorMsg method and remove the throws clause.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Farakh khan wrote:
it again throws

Ofcourse, because the errorMsg in the nested catch block can still throw Exception, which you have to handle. You can do this recursively as far as you like...

In my opinion, you should make errorMsg not throw Exception. Remove the "throws Exception" in the declaration of the errorMsg method, and handle exceptions that happen inside that method, inside the method itself (with a try-catch block). (Don't recursively call errorMsg() to handle them however!).
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

So change the errorMsg method to catch the exception instead of throwing it:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3426
    
  12
Bit slow today Rob
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Finally I done like this:



I am grateful to all who extended their help

Thanks again & best regards
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Remove the "throws Exception" in line 13, so that you don't have to have a nested catch block and you can just do this:

And don't call errorMsg recursively, as you are doing in line 24. You will get an infinite recursive loop when you do this, as Rob warned you for.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
That's all what I was looking for. Thanks again from the bottom of my heart

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3426
    
  12
And more generally, always try to avoid throwing or catching Exception. Make the exception type as specific as possible. Especially as Java 7 allows you to do multiple catches in a single statement.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Joanne Neal wrote:And more generally, always try to avoid throwing or catching Exception. Make the exception type as specific as possible. Especially as Java 7 allows you to do multiple catches in a single statement.


Thanks for addition
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: java:280: 'void' type not allowed here
 
Similar Threads
While replacing char of soap response then it throws error
package javax.servlet.annotation does not exist
175: warning: BASE64Encoder is internal proprietary API and m ay be removed in a future release
urgent help in java mail
How to convert XML into HTML?