Search code examples
java-metry-catchnokia-s40

Try/Catch seems to be doing nothing in Java ME


Consider this:

public class Minesweeper extends MIDlet implements CommandListener {
  public static String error = "";

  public void startApp() throws MIDletStateChangeException {

    try{
        int int = 5;
    } catch (Exception e) {
        error = e.toString();
    }
  }
}

int is an invalid name for an int, so surely the error should be caught and registered in error? (This error is there on purpose so I can catch it)

You've probably guessed it though, the error doesn't seem to be caught, and the app stops with a java/lang/Error Unresolved compilation problem: syntax error on token 'int'....

What am I doing wrong.

(BTW, this was just a test so I knew I could catch errors properly, I'm obviously not going to use that code in a final version.)


Solution

  • Here is your problem - "int int = 5;" - you cannot use "int" as a variable name ;)

    You are trying to use a reserved word as a variable name. The compilation problem has nothing to do with your try-catch block working or not working, the compiler never gets that far.