Hello Guest

try/catch LWJGL exception

  • 9 Replies
  • 4855 Views
try/catch LWJGL exception
« on: May 09, 2014, 23:54:19 »
Hi,
I've been doing a simple error log for my game, basically when an exception is catch it writes down the traces on a log .txt file.
It works fine with java exception (like IO etc...) but it seems that my try/catch don't catch the LWJGL exceptions (it actually print the trace in the console).
I think I'm missing something here. (I don't show code since it's just a try catch)
Any idea?

*

Offline Cornix

  • *****
  • 488
Re: try/catch LWJGL exception
« Reply #1 on: May 10, 2014, 07:39:54 »
Its probably caused by the flying spaghetti monster. You have to sacrifice some goats in order to fix it.

Or maybe, you just show us the code you used so we can give you any useful advice instead of speculations.

Re: try/catch LWJGL exception
« Reply #2 on: May 10, 2014, 12:09:10 »
Ahah, as I said i'm just talking of a simple:
Code: [Select]
try{
  //GL stuff
}
catch(LWJGLException e){
  //handle exception
}

For example:
Code: [Select]
try {
  Controllers.create();
  System.out.println("Number of controller: " + Controllers.getControllerCount());
} catch (LWJGLException e) {
  ErrorLog.writeToErrorLog(e.getStackTrace());
  e.printStackTrace();
}

So be clear, if I do that kind of try/catch:
Code: [Select]
try {
bw = Files.newBufferedWriter(errorLog.toPath(), charset, op);
bw.write(dateFormat.format(date));
bw.newLine();
for(StackTraceElement trace: error){
bw.write(trace.toString());
bw.newLine();
}
bw.close();
} catch (IOException e) {
System.out.println("Yeah");
}

"Yeah!" will print, but when I do the same with lwjgl methods and I do exception on purpose it will not.
« Last Edit: May 11, 2014, 13:47:10 by Hanksha »

*

Offline Cornix

  • *****
  • 488
Re: try/catch LWJGL exception
« Reply #3 on: May 11, 2014, 19:47:41 »
In your example you use System.out for your output stream.
Are you sure that your "ErrorLog" uses the same stream to write its data to?

Re: try/catch LWJGL exception
« Reply #4 on: May 11, 2014, 22:12:17 »
The problem is not what is inside the the catch where I handle an exception. (cause it works fine with simple java exceptions)
What I am saying is that when I'm using lwjgl methods what I write in the catch (whatever it is) is not read; maybe it is handle by a try catch inside the method itself, is there a way to overwrite that?

*

Offline Yuri6037

  • ***
  • 104
  • Check out our website : http://www.sldt-team.net !
    • SLDT
Re: try/catch LWJGL exception
« Reply #5 on: May 13, 2014, 08:12:47 »
I don't realy understand ; i already made some kind of error management. My error management is working as a thread : All thrown exceptions are catched by a catch(Exception e) so i can after call my CrashReporter.instance.generateNewCrashReport. In all cases the LWJGLException is correctly working, maybe you're not using it right... If you want i can give you a code example that is working :
Code: [Select]
        try {
            if (isWindowed){
                Display.setDisplayMode(new org.lwjgl.opengl.DisplayMode(sizeX, sizeY));
            } else {
                Display.setDisplayMode(Display.getDesktopDisplayMode());
            }
            Display.setFullscreen(!isWindowed);
            Display.setTitle(gameName);
            Display.create();
            log.info("Starting LWJGL - OpenAL...");
            AL.create();
            log.info("LWJGL - OpenAL successfully started !");
            Mouse.create();
            Keyboard.create();
            initOpenGL();
            init();
            ByteBuffer[] shit = new ByteBuffer[2];
            if (getIconPackage() == null) {
                shit[0] = renderEngine.mountTexture(SLDTGame.class.getResourceAsStream("icon16.png"));
                shit[1] = renderEngine.mountTexture(SLDTGame.class.getResourceAsStream("icon32.png"));
            } else {
                shit[0] = renderEngine.mountTexture(ClassLoader.getSystemResourceAsStream("./" + getIconPackage() + "/gameIcon16.png"));
                shit[1] = renderEngine.mountTexture(ClassLoader.getSystemResourceAsStream("./" + getIconPackage() + "/gameIcon32.png"));
            }
            Display.setIcon(shit);
            //Mouse.setNativeCursor(new org.lwjgl.input.Cursor(16, 16, 0, 0, 1, getHandMousePointer(), null));
            while (!Display.isCloseRequested()){
                Display.update();
                gameTimer.updateTimer();
                GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
                for (int j = 0; j < gameTimer.elapsedTicks; j++) {
                    update();
                }
                render();

                updateFPS();
            }
            closeGame();
        } catch (Exception e){
            onGameCrash(e);
        }
Imagine that this function is called like in a thread named "GameName" - Main Thread This is the thread of OpenGL. When something in the game code is generating an exception, the exception is always catched by onGameCrash(e). This function is part of my game engine it's called at every thrown exception, you can do whatever you want with that.
If you want to know more, i can tell you that onGameCrash will call an ExceptionHandler interface that the game must have implemented.
For Exaple :
Code: [Select]
package fr.scrable.main;

import fr.sldt.gameEngine.ExceptionHandler;

public class ScrableGameExceptionHandler implements ExceptionHandler{
    public void handleException(Exception e) {
        e.printStackTrace();
        System.exit(0);
    }
}
This is the code witch will be called at every exception thrown. In my case i just print the exception and i simple hard close the game, like CTRL + ALT + SUPPR...


Hope i helped you,
Yuri6037

Re: try/catch LWJGL exception
« Reply #6 on: May 13, 2014, 09:46:47 »
Maybe that's the problem, I should put it in another thread.

*

Offline abcdef

  • ****
  • 335
Re: try/catch LWJGL exception
« Reply #7 on: May 13, 2014, 16:17:05 »
Catching exception e is not very good, its always good to know exactly what is going on and to then determine what to do ad to have the full stack trace when investigating. java.util.Logger is a very simple way to catch exceptions and log them (a simple version of Log4j)

I'm not sure why your code is not picking up the exception, it would be useful to see how you are faking an exception being thrown.

*

Offline Yuri6037

  • ***
  • 104
  • Check out our website : http://www.sldt-team.net !
    • SLDT
Re: try/catch LWJGL exception
« Reply #8 on: May 13, 2014, 19:30:45 »

*

Offline matanui159

  • *
  • 30
  • KABOOM
Re: try/catch LWJGL exception
« Reply #9 on: August 17, 2014, 01:21:59 »
Maybe just as an idea, still have your error logger to log.txt, but also redirect you error stream to a file like so:

Code: [Select]
System.setErr(new PrintStream(new FileOutputStream("log.txt")));
This won't be as good though... But it catches all errors
Also a tip, when making a game which has error logging like yours, its a good idea to have info like Operating System, Java Version and OpenGL version.
ALGORITHM
A word used by programmers when they do not want to explain what they did.

WEE :)