• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java:280: 'void' type not allowed here

 
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Rancher
Posts: 3742
16
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!).
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So change the errorMsg method to catch the exception instead of throwing it:
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bit slow today Rob
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Finally I done like this:



I am grateful to all who extended their help

Thanks again & best regards
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's all what I was looking for. Thanks again from the bottom of my heart

 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
a wee bit from the empire
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic