java(异常)的一点问题。。。

来源:百度知道 编辑:UC知道 时间:2024/06/20 22:20:26
public class TestDe{
public static void main(String[] args) {
TestDe k = new TestDe();
k.m(0);

public void m(int i) throws ArithmeticException{
if(i==0)
throw new ArithmeticException("被除数为0");
}
}

}

为什么运行时候说
public void m(int i) throws ArithmeticException{
是非法表达式?
需要“;”
不是语句??
我这段代码写错了吗?。。。。

你是方法中写方法,你main方法的大括号都给你写到下面的方法后去了~

【错误原因】
1、main()方法少了一个“}”.
2、if语句也少了一个“{”.(或者去掉“}”)
【改正后代码】
public class TestDe{
public static void main(String[] args) {
TestDe k = new TestDe();
k.m(0);
}
public void m(int i) throws ArithmeticException{
if(i==0) {
throw new ArithmeticException("被除数为0");
}
}

}
编译可通过,希望我的回答对你能有帮助。

是写错了,方法里就不要再去定义方法了
public class TestDe{
public static void main(String[] args) {
TestDe k = new TestDe();
k.m(0);
}
public void m(int i) throws ArithmeticException{
if(i==0)
throw new ArithmeticException("被除数为0");

}

}

写做啦~
public class TestDe{
public static void main(String[] args) {
TestDe k = new TestDe();
k.m(0);

}

public void m(int i) throws ArithmeticException{
// TODO 自动生成方法存根
if(i==0)
throw new ArithmeticExce