Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ExceptionCollection

 
Lila Fowler
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I didn't know about that--cool.
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1308
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lila Fowler wrote:But it only displays the first exception that is thrown & ignores the others.


try this :

 
Lila Fowler
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope sorry, tried moving the methods around but i still get the same problem, only the first error gets displayed.
Thanks though.
 
salvin francis
Bartender
Posts: 1308
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
read comment, sorry i forgot to place comment there before your response...
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys it all seems to be working now.
 
Lila Fowler
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have also tried

But still only picks up the first exception
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Output:
 
Lila Fowler
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duh, Okay thanks I completely forgot about that part. knew I was missing something stupid
Thanks again
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic