大家都知道在非J2ME程序中,捕捉异常后用ex.printStackTrace()可以得到错误堆栈,错误堆栈中除了有错误信息(原因)外,还有抛出异常的位置在源文件的行数,从而可以准确地定位错误。
但是在J2ME程序中,用ex.printStackTrace()输出的信息似乎并不是抛出异常的位置在源文件的准确行数,现在有一个简单示例如下:
类MIDletTest2.java:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class MIDletTest2 extends MIDlet
{
MyCanvas myCan;
public MIDletTest2()
{
myCan = new MyCanvas();
}
protected void pauseApp()
{
}
protected void startApp()
{
Display.getDisplay(this).setCurrent(myCan);
}
protected void destroyApp(boolean parm1)
{
}
}
类MyCanvas.java:
import javax.microedition.lcdui.*;
public class MyCanvas extends Canvas
{
Image img;
public MyCanvas()
{
}
protected void paint(Graphics g)
{
try
{
img = Image.createImage("/err/err.png");//抛出异常的地方
}
catch (Exception ex)
{
ex.printStackTrace();
}
g.drawImage(img, 0, 0, g.TOP|g.LEFT);
}
}
err.png是不存在的文件,很明显当首次调用paint时,会因为找不到图片文件而抛出异常,下面看抛出异常的错误堆栈信息:
java.io.IOException: Couldn't find resource
at javax.microedition.lcdui.Image.<init>(+36)
at javax.microedition.lcdui.Image.createImage(+8)
at MyCanvas.paint(+6)
at javax.microedition.lcdui.Canvas.paint(+149)
at javax.microedition.lcdui.Canvas.serviceRepaints(+8)
at javax.microedition.lcdui.Canvas.handleShown(+42)
at javax.microedition.lcdui.Canvas.handleCurrent(+26)
at javax.microedition.lcdui.Display.setCurrent(+81)
at MIDletTest2.startApp(+11)
at javax.microedition.midlet.MIDletInvoker.invokeStartApp(+4)
at com.symbian.midp.compatibility.runtime.MIDletScheduler.startMIDlet(+29)
at com.symbian.midp.compatibility.runtime.MultiMIDletScheduler.run(+17)
接下来就看不懂了,首先每行的括号里面的+号是什么意思,此外+号后面的数字并不是抛出异常的位置在源文件中的准确行数。请大家给予解答。
但是在J2ME程序中,用ex.printStackTrace()输出的信息似乎并不是抛出异常的位置在源文件的准确行数,现在有一个简单示例如下:
类MIDletTest2.java:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class MIDletTest2 extends MIDlet
{
MyCanvas myCan;
public MIDletTest2()
{
myCan = new MyCanvas();
}
protected void pauseApp()
{
}
protected void startApp()
{
Display.getDisplay(this).setCurrent(myCan);
}
protected void destroyApp(boolean parm1)
{
}
}
类MyCanvas.java:
import javax.microedition.lcdui.*;
public class MyCanvas extends Canvas
{
Image img;
public MyCanvas()
{
}
protected void paint(Graphics g)
{
try
{
img = Image.createImage("/err/err.png");//抛出异常的地方
}
catch (Exception ex)
{
ex.printStackTrace();
}
g.drawImage(img, 0, 0, g.TOP|g.LEFT);
}
}
err.png是不存在的文件,很明显当首次调用paint时,会因为找不到图片文件而抛出异常,下面看抛出异常的错误堆栈信息:
java.io.IOException: Couldn't find resource
at javax.microedition.lcdui.Image.<init>(+36)
at javax.microedition.lcdui.Image.createImage(+8)
at MyCanvas.paint(+6)
at javax.microedition.lcdui.Canvas.paint(+149)
at javax.microedition.lcdui.Canvas.serviceRepaints(+8)
at javax.microedition.lcdui.Canvas.handleShown(+42)
at javax.microedition.lcdui.Canvas.handleCurrent(+26)
at javax.microedition.lcdui.Display.setCurrent(+81)
at MIDletTest2.startApp(+11)
at javax.microedition.midlet.MIDletInvoker.invokeStartApp(+4)
at com.symbian.midp.compatibility.runtime.MIDletScheduler.startMIDlet(+29)
at com.symbian.midp.compatibility.runtime.MultiMIDletScheduler.run(+17)
接下来就看不懂了,首先每行的括号里面的+号是什么意思,此外+号后面的数字并不是抛出异常的位置在源文件中的准确行数。请大家给予解答。