异常处理
废话不说,代码如下:
package firstproject;
public class yichangchuli {
}
class MyMath {
static int a; //此处必须用static修饰(公开),不能用private修饰( 私有);
static int b;
static int result=0;
public MyMath(int a,int b) {
MyMath.a=a; //可以用 this.a=a;
MyMath.b=b;
}
public void MyMath() {
result=a/b;
System.out.println(result);
}
}
class TestDemo {
public static void main(String args[]) {
MyMath m=new MyMath(1,0);
try {
m.MyMath();
} catch (Exception e) {
System.out.println("程序运行出错了哦,被除数不能为零!!!");
System.exit(1) ; // 程序退出
}
}
}
运行结果如下: