Page 1 of 1

Error Handling

PostPosted: 21 Aug 2009, 17:38
by mtgrares
So Dennis mentioned that Silly Freak greatly improved the error reporting system? That sounds really good. It would be nice when there was an error if MTG Forge saves the error to a file or sends an e-mail if the player is online.

Re: Error Handling

PostPosted: 21 Aug 2009, 20:11
by silly freak
error saving is not automatic, but a save button is integrated. the error message also contains your mail and the forum address, but the user can also just click the message away

and also errors while file saving are handled this way. so, if you have some read-only location, you can pop up as many error dialogs as you want^^

Re: Error Handling

PostPosted: 27 Sep 2009, 11:01
by silly freak
I thought I'd reuse this thread, it's a small addition to the error handling.

Right now, every error is caught by us and reported by ErrorViewer. That's good, but doesn't prevent some errors from slipping through.

the new class forge.error.ExceptionHandler prevents exactly that. It has two mechanisms: One for Java, when a thread terminates because of an exception, and one for AWT. AWT catches the exception and by default prints to System.err. This class alters the behavior to use ErrorViewer.
Code: Select all
/**
 * ExceptionHandler.java
 *
 * Created on 27.09.2009
 */

package forge.error;


import java.lang.Thread.UncaughtExceptionHandler;


/**
 * This class handles all exceptions that weren't caught by showing the error to the user.
 */
public class ExceptionHandler implements UncaughtExceptionHandler {
    static {
        //Tells Java to let this class handle any uncaught exception
        Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler());
        //Tells AWT to let this class handle any uncaught exception
        System.setProperty("sun.awt.exception.handler", ExceptionHandler.class.getName());
    }
   
    /**
     * Call this at the beginning to make sure that the class is loaded and the static initializer has run
     */
    public static void registerErrorHandling() {
        System.out.println("Error handling registered!");
    }
   
    @Override
    public void uncaughtException(Thread t, Throwable ex) {
        ErrorViewer.showError(ex);
    }
   
    /**
     * This Method is called by AWT when an error is thrown in the event dispatching thread and not caught.
     */
    public void handle(Throwable ex) {
        ErrorViewer.showError(ex);
    }
}
to use the class, you just have to add the import and insert another line right at the top of Gui_NewGame.main:
Code: Select all
ExceptionHandler.registerErrorHandling();

Re: Error Handling

PostPosted: 27 Sep 2009, 16:34
by DennisBergkamp
Cool, I've added it.

Is this where I put it though?
Code: Select all
public static void main(String[] args) {
        ExceptionHandler.registerErrorHandling();
       try {
            Object[] o = UIManager.getInstalledLookAndFeels();
Or do I add it into the try block?

Re: Error Handling

PostPosted: 27 Sep 2009, 16:54
by silly freak
it doesn't really matter, i think. the things done in this call (that includes the static initializer) aren't critical. i've put it outside.