Java/J2ME/Exception
Test Exception
/*--------------------------------------------------
* TestException.java
*
* Example from the book: Core J2ME Technology
* Copyright John W. Muchow http://www.CoreJ2ME.ru
* You may use/modify for any non-commercial purpose
*-------------------------------------------------*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class TestException extends MIDlet implements CommandListener
{
private Display display; // Reference to Display object for this MIDlet
private Form fmMain; // A Form
private Command cmExit; // A Command to exit the MIDlet
private boolean safeToExit = false; // Is it safe to exit the MIDlet?
// MIDlet constructor
public TestException()
{
display = Display.getDisplay(this);
cmExit = new Command("Exit", Command.SCREEN, 1);
fmMain = new Form("Test Exception");
fmMain.addCommand(cmExit);
fmMain.setCommandListener(this);
}
// Called by application manager to start the MIDlet.
public void startApp()
{
display.setCurrent(fmMain);
}
// We are about to be placed in the Paused state
public void pauseApp()
{
}
// We are about to enter the Destroyed state
public void destroyApp(boolean unconditional) throws MIDletStateChangeException
{
System.out.println("Inside destroyApp()");
// If we do not need to unconditionally exit
if (unconditional == false)
{
System.out.println("Requesting not to be shutdown");
throw new MIDletStateChangeException("Please don"t shut me down.");
}
}
// Check to see if the Exit command was selected
public void commandAction(Command c, Displayable s)
{
if (c == cmExit)
{
try
{
// Is it ok to exit?
if (safeToExit == false)
destroyApp(false);
else
{
destroyApp(true);
notifyDestroyed();
}
}
catch (MIDletStateChangeException excep)
{
safeToExit = true; // Next time, let"s exit
System.out.println(excep.getMessage());
System.out.println("Resuming the Active state");
}
}
}
}