• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Writing all errors ever occuring to file

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm working on a personal project that is starting to get some size, 344kb as *.jar, split on 49 class files.
I might be releasing the program officially when it's done, so I want the program to log all errors so that the users can send me their log and help me fix any problems.

I've put in an error handling class that will print information of any exception recieved to the log file, plus show a dialog to the user about it.
It works fine catching any errors occuring when starting the program and loading files, where I have already put try/catch.
But I don't know what to do with the rest of the program, if I am to log all errors ever occuring. I'm not looking for methods to handle the errors, just to send the errors to the error handling class so that it can write them to file.
Since the program has several thousand lines already, I hope there is some way that doesn't require me to insert extra methods\lines "everywhere"...

Is there any way to just tell the program "if any exception occurs anywhere, call method"?
Would it be a good idea to make all methods throw exception?
Any ideas?
 
Marshal
Posts: 74054
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You only want to declare Exceptions from methods which might actually thrown them.
Probably best to design an Exception class for your application, which you can throw from whichever method might need it; you can throw all sorts of subclasses of it and catch all your Exceptions with catch(NoriException ne)...
It's probably a bad idea to try catching every Exception in a single catch(Exception exc), however.
Don't log an Exception and rethrow it; that simply means logging it twice.

How about a logging class? Pass the Exceptions to it from every catch, and let the logger log them. You can pass a this reference too, so the logger can see where the Exception occurred. Maybe make the log method static?? Or maybe have a getInstance method like a singleton??

Anybody else?
 
author & internet detective
Posts: 40747
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kari,
Do you want all exceptions or all fatal exceptions? If the later, you could add the exception handling to the main method. If you want all exceptions even those handled, you could add the exception handling to the main method and the catch blocks.

An AOP (aspect oriented programming) library could intercept all these exceptions for you. I think they are available for standalone programs, but I'm not positive. That would increase your memory footprint for the user though, so it seems like overkill here.
 
Kari Nordmann
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to log all exceptions, regardless how serious they are. I'm not looking for ways to make the program continue running through all exceptions, but to know what exceptions occur so that I can fix the cause in the code.
User mistakes causing exceptions shouldn't be a problem in this program, exceptions are usually caused by me having overlooked something when doing changes in the code. I usually testrun the program on a different computer than the one I program it on too.

I have made the main throw exception from the start on, but it doesn't do anything at all.


This is what you ment, Jeanne?
I'm thinking it doesn't work because errors often happen after those methods are done and the user starts doing something.

Edit: I tried adding throws Exception to one of the methods and forced an exception. The result confirmed my suspicion that using try\catch might not be what I want, because it stops the program even if the exception is not critical.
[ October 11, 2008: Message edited by: Kari Nordmann ]
 
Marshal
Posts: 22457
121
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
Since Java 5 you can use Thread.UncaughtExceptionHandler to catch any exception you don't catch yourself:


Although the default is null, Java still does the following:

You can use that to mimic the default behaviour beside your logging.
[ October 11, 2008: Message edited by: Rob Prime ]
 
Jeanne Boyarsky
author & internet detective
Posts: 40747
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kari,
I was thinking of one big try/catch and all existing content of the main method in it. This doesn't meet your requirement though.
 
Kari Nordmann
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Prime:
[QB]Since Java 5 you can use Thread.UncaughtExceptionHandler to catch any exception you don't catch yourself:
QUOTE]

Thanks, works perfect so far Exceptions are logged, error message dialog pops up, and the program doesn't stop

 
Campbell Ritchie
Marshal
Posts: 74054
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kari Nordmann:
[
Thanks, works perfect so far



Well done
 
This cake looks terrible, but it tastes great! Now take a bite out of this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic