public class Ex3{int x;public static void main(String ar[])throws Exception{int y=0;Ex3 c=new Ex3();try{y=3/c.x;y=y+1;}catch(Exception e){System.out.println("errors occur!");System.out.println("getMessage="+e.getMessage());System.out.println("toStrin
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/30 19:16:32
public class Ex3{int x;public static void main(String ar[])throws Exception{int y=0;Ex3 c=new Ex3();try{y=3/c.x;y=y+1;}catch(Exception e){System.out.println("errors occur!");System.out.println("getMessage="+e.getMessage());System.out.println("toStrin
public class Ex3{
int x;
public static void main(String ar[])throws Exception{
int y=0;
Ex3 c=new Ex3();
try{
y=3/c.x;
y=y+1;}catch(Exception e){
System.out.println("errors occur!");
System.out.println("getMessage="+e.getMessage());
System.out.println("toString="+e.toString());}
System.out.println("program ends ok");
System.out.println("the y is:"+y);
}
}
注释一下每行,不太懂try catch的用法
public class Ex3{int x;public static void main(String ar[])throws Exception{int y=0;Ex3 c=new Ex3();try{y=3/c.x;y=y+1;}catch(Exception e){System.out.println("errors occur!");System.out.println("getMessage="+e.getMessage());System.out.println("toStrin
public class Ex3{ //类
int x;
public static void main(String ar[])throws Exception{ //主函数,
//throws Exception是遇到异常抛出,如果其他类或函数用到可以用catch捕
//捉到
int y=0;
Ex3 c=new Ex3();
try{ //在try里面填要捕捉异常的过程
y=3/c.x;
y=y+1;}catch(Exception e){ //catch捕捉,例如如果c.x=0就会抛出一个分母
//不应该为0的异常
System.out.println("errors occur!");
System.out.println("getMessage="+e.getMessage()); //用e.getMessage()可
//以得到异常的信息,如空指针之类的提示
System.out.println("toString="+e.toString());}//打印出现的异常名,上面
//那个是异常的提示信息,不同
System.out.println("program ends ok");
System.out.println("the y is:"+y); //打印y=多少,比如=0的话将会看到异
//常相关信息及y的值
}
很简单,可以自己再看看
}