您的位置:首页 > 其它

两种表示异常类的方法

2017-05-16 17:25 225 查看
1 用 try-catch的方法。。

package why;
import java.util.*;
public class iiii {
public static void main(String args[])
{    int n=0,m=0,t=0;
try
{  t=9999;
m=Integer.parseInt("8888");
n=Integer.parseInt("13a");
System.out.println("我没有机会输出");
}
catch(Exception e)
{   System.out.println("eror");
System.out.println(m);
}
}
}


2 自定义异常类

要注意如果错误就抛出。然后在catch里处理

package why;
import java.util.*;
class Myexception extends Exception
{  String message;
Myexception (int n)
{     message=n+"不是正数";
}
public String getMessage()
{  return message;
}
}
public class lovestick {
public static void main(String args[])
{    try
{    int m=2;
int s=-1;
if(s<0)
{
Myexception xx=new Myexception(s);
throw(xx);
}
}
catch(Myexception xx)
{    System.out.println(xx.getMessage());
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: