aspose file tools*
The moose likes Java in General and the fly likes ExceptionCollection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "ExceptionCollection" Watch "ExceptionCollection" New topic
Author

ExceptionCollection

Lila Fowler
Ranch Hand

Joined: Jul 31, 2009
Posts: 84
Hi all, im trying to get a dialog box to pop up every time a java exception has been found in my program, asking the user if he would like to report the exception but i cant figure out how it is possible to collect these exceptions without either a) Calling a method in every single catch.(redundant & would take a few days just to put it in every try catch) Or b) Reading it from the log file as the error could occur that is not present in the log & the same errors would be repeated every time the log file is updated.

Any suggestions would be helpful, thanks


Courage is not the absence of fear but rather the judgment that something is more important then fear. ~ Ambrose Redmond
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

If you are using Java 5.0 or higher you can use Thread.setDefaultUncaughtExceptionHandler. For example, to achieve the default:
Remove the first call to System.err and see the difference.

Make sure that you are executing your user interface code on the Event Dispatcher Thread. See concurrency in Swing.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Oh, I didn't know about that--cool.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

I can't remember how I stumbled upon it, but it was a welcome addition. Just think of all the uncaught RuntimeExceptions thrown in event handlers that cause strange behaviour and you don't even know why because the Java app is running without a console screen... Now you can catch them and display them, or write them to a log file, or anything you want.
Lila Fowler
Ranch Hand

Joined: Jul 31, 2009
Posts: 84
Hi, again, thanks for the reply but im still a kind of confused about how this method works. I created a test class, one with a runtime exception & one with a ArithmeticException. But it only displays the first exception that is thrown & ignores the others.

Here are 2 methods: (application still runs irrelevant, the ex don't crash the program)



Sorry if i'm missing something really stupid, but i can't seem to figure it out.
Thanks again
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Lila Fowler wrote:But it only displays the first exception that is thrown & ignores the others.


try this :



My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Lila Fowler
Ranch Hand

Joined: Jul 31, 2009
Posts: 84
Nope sorry, tried moving the methods around but i still get the same problem, only the first error gets displayed.
Thanks though.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

read comment, sorry i forgot to place comment there before your response...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

The exception still causes the main method to be exited. You are not catching the exceptions right where they occur; only at the very top of the chain, where they otherwise would remain uncaught by the main method (or thread run method). Try this:
The new thread is started before the exception and it creates its own exception. Now you have two exceptions being caught.

Note that the synchronized is only there to make sure the printing to System.err is done as one atomic block.
Lila Fowler
Ranch Hand

Joined: Jul 31, 2009
Posts: 84
Thanks guys that seems to be working perfectly now expect that it is ignoring sql exceptions. (That would be because its not really a java exception right?)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

It most definitely is a Java exception. The UncaughtExceptionHandler only catches exceptions you have not caught yourself though. If you catch SQLException (because you have to, perhaps) then you will need to duplicate the code from the UncaughtExceptionHandler, or if possible invoke its own uncaughtException exception. You could do this by turning your class into a singleton class:
Installing it is easy:
Catching exceptions is just as easy:
Lila Fowler
Ranch Hand

Joined: Jul 31, 2009
Posts: 84
Thanks guys it all seems to be working now.
Lila Fowler
Ranch Hand

Joined: Jul 31, 2009
Posts: 84
Hey, sorry to use an old thread but since its on exactly the same topic i thought it mite be a better idea then starting a new one.

This was working perfectly but my code was accidentally deleted & i am trying to get it to work again.
I can't get it to display both exceptions instead of only the first one. --> I tried both runnable and Thread.UncaughtExceptionHandler but i can for the life of me figure out what i did last time so that is was working. If i use both runnable and Thread.UncaughtExceptionHandler at the same time it cant find my main method so i'm most prob doing something really stupid. Here is my code;




Sorry about this, thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

If I run your code it shows two calls to the anonymous handler you create on line 22. This makes sense - both the thread and the call to mainClass are likely to finish before you install the new handler.
Lila Fowler
Ranch Hand

Joined: Jul 31, 2009
Posts: 84
I also tried it like this if that is what you meant:



I have this idea that i should be using something like:



Or am I totally mixing it up?
Thanks
Lila Fowler
Ranch Hand

Joined: Jul 31, 2009
Posts: 84
I have also tried

But still only picks up the first exception
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Output:
Lila Fowler
Ranch Hand

Joined: Jul 31, 2009
Posts: 84
Okay now i'm confused, i'm trying to get it to print all three exceptions once, not just the first exception twice.. Would that be possible?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

You can't make method mainClass throw more than one exception. throwEx2() throws an exception which aborts method mainClass. This simply throws the exception further up the chain until it reaches either the main method or the run method from the thread. That's the place where the UncaughtExceptionHandler comes into action, not sooner.

So the flow of the thread is this:
Even if you have a handler installed, the exception will still be passed all the way up the chain, until it reaches the outer-most call - the main method, or the run() method of a thread. That's the only place where the handler is listening for exceptions, not directly where the exception is thrown.
Lila Fowler
Ranch Hand

Joined: Jul 31, 2009
Posts: 84
Duh, Okay thanks I completely forgot about that part. knew I was missing something stupid
Thanks again
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ExceptionCollection