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?
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??
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.
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 ]