public class SelfGenerateException extends Exception
{
SelfGenerateException(String msg){
super(msg); //调用Exception的构造方法
}
static void throwOne() throws SelfGenerateException
{
int a = 1;
if (a==1) //如果a为1就认为在特定应用下存在异常,改变执行路径,抛出异常
{throw new SelfGenerateException(“a为1”);}
}
public static void main(String args[])
{
try
{throwOne();}
catch(SelfGenerateException e)
{e.printStackTrace();}
}
}
自定义一个异常类,并且在程序中主动产生这个异常类对象。
最新推荐文章于 2022-11-09 00:27:04 发布