import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class ExcepTest extends MIDlet implements CommandListener{ private Display display; // a display object private Command Exit; // a button to exit the midlet private Form Main; // create aform private boolean reallyExit = false; // do you really want to exit // midlet constructor public ExcepTest(){ display = Display.getDisplay(this); Main = new Form("Exception Testing"); Exit = new Command("Exit",Command.SCREEN, 1); Main.addCommand(Exit); Main.setCommandListener(this); } // start the midlet public void startApp(){ display.setCurrent(Main); } public void pauseApp(){ // no action } public void destroyApp(boolean unconditional) throws MIDletStateChangeException { System.out.println("Inside destroyApp()"); // if we do not need unconditional exit if (unconditional ==false){ System.out.println("Request not to be shutdown"); throw new MIDletStateChangeException("Do not shut me down"); } } // check if the exit command was slected public void commandAction(Command c, Displayable s) { if (c == Exit){ try{ // are we ready to exit? if (reallyExit ==false) destroyApp(false); else{ destroyApp(true); notifyDestroyed(); } } catch (MIDletStateChangeException excep){ reallyExit = true; // second time we will really exit System.out.println(excep.getMessage()); System.out.println("Back to active state"); } } } }