0

I have server-client application which has a several users. My client is a big application and there is a lot of exception handlings inside of it. I want to collect and transfer all uncaught exceptions whichs are unhandling. For example, when a client throw an uncaught exception like NullPointer or ArrayIndexOutOfBounds exception, i want to transfer the message to the server and write it to a file. Is it possible?

rdonuk
  • 3,741
  • 16
  • 37
  • think about what might happen if contacting the server throws an exception – Philipp Sander Apr 25 '14 at 11:50
  • catch (IOException e ){ write.somewhere (e); } – Watsche Apr 25 '14 at 11:52
  • I think what you really want is to add some proper logging to the application and configure the logging framework to write all errors and stack traces to some file? – tobias_k Apr 25 '14 at 11:53
  • @Philipp Sander Client is an swing application and i want to know the errors about UI. if there is an connection error, i know what will i do but if there is an UI error whichs case's not clear it s hard for me. im saying to users "do not close your console and sent errors to me" but they are always forget it. – rdonuk Apr 25 '14 at 12:01
  • Possible duplicate of [Java: Global Exception Handler](http://stackoverflow.com/questions/1548487/java-global-exception-handler) and [Catch exceptions in javax.swing application](http://stackoverflow.com/q/6827646/230513). – trashgod Apr 25 '14 at 12:03
  • Any advice for logging? I must do it with minimum line of code. – rdonuk Apr 25 '14 at 12:03

3 Answers3

1

Any advice for logging?

As shown here and here, you can add a default uncaught exception handler. As shown here, you can display the stack trace in a dialog.

Community
  • 1
  • 1
trashgod
  • 196,350
  • 25
  • 213
  • 918
0

If you want to print the entire stack trace, use printStackTrace:

FileWriter fw = new FileWriter("exception.txt", true);
PrintWriter pw = new PrintWriter(fw);
e.printStackTrace (pw);
Philipp Sander
  • 9,615
  • 3
  • 41
  • 75
Sunny
  • 298
  • 2
  • 13
0

Yes, It is possible but not advisable. As Swing works on a Single Threaded Model, all the events are handled by EDT so for a possible solution you need to capture these exceptions at the place from where these events are dispatched.

EventQueue is the place that keeps track of all the events, so you can override it in your application (NOT A GOOD IDEA) and catch all the exceptions there and send to server from there in a separate thread.

And also you need to capture exceptions in all the threads your application is creating (other than swing threads).

More appropriate solution would be to log them at client side only and these log file can later be scanned to check for the exceptions.

Hope this helps.

Sanjeev
  • 9,741
  • 2
  • 19
  • 33