atskyline-BLOG
心里有就有,心里没有就没有。
posts - 11,comments - 2,trackbacks - 0

其实就是J2SE异常的那些东西.

代码中把字符串s样转换成int 明显会抛出异常

我们定义了一个继承于Exception 的自定义异常类

并将异常抛给客户端处理

import java.util.Date;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class MIDlet1 extends MIDlet
{

	protected void startApp() throws MIDletStateChangeException
	{
		String s="a";
		try
		{
			this.print(s);
		} catch (CustomException e)
		{
			System.out.println(e.getMessage());
			System.out.println(e.getDate());
		}

	}
	public void print(String str) throws CustomException
	{
		try
		{
			int intValue = Integer.parseInt(str);
			int result = intValue*intValue;
			System.out.println(result);
		}
		catch(Exception ex)
		{
			CustomException ce =new CustomException("错误",new Date());
			throw ce;
		}

	}
	
	class CustomException extends Exception
	{
		private Date date;
		public CustomException(String message,Date date)
		{
			super(message);
			this.date=date;
		}
		public Date getDate()
		{
			return this.date;
		}
	}
	
	protected void destroyApp(boolean arg0) throws MIDletStateChangeException
	{

	}

	protected void pauseApp()
	{

	}

}
posted on 2008-07-16 21:17 atskyline 阅读(275) 评论(0)  编辑 收藏 引用 所属分类: J2ME笔记
只有注册用户登录后才能发表评论。