异常问题求助

zhaoyunf2005 2007-12-26 06:48:23
最近在学习马士兵的java视频,里面有个异常的例子程序,敲进去后怎么也编译不过去,请各位高手指点!
import java.io.*;

public class Ex {
public static void main(String[] args) {
Ex te = new Ex();
te.m(0);

/*
try {
new TestEx().m(0);
} catch (ArithmeticException ae) {
ae.printStackTrace();
System.out.println("出错了");
}
*/
void m(int i) throws ArithmeticException {
if(i == 0)
throw new ArithmeticException("被除数为0");
}
}
}

命令行窗口显示错误信息:

D:\javawork>javac Ex.java
Ex.java:16: 非法的表达式开始
void m(int i) throws ArithmeticException {
^
Ex.java:16: 需要 ';'
void m(int i) throws ArithmeticException {
^
Ex.java:16: 需要 ';'
void m(int i) throws ArithmeticException {
^
Ex.java:16: 不是语句
void m(int i) throws ArithmeticException {
^
Ex.java:16: 需要 ';'
void m(int i) throws ArithmeticException {
^
5 错误
...全文
83 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhaoyunf2005 2007-12-26
  • 打赏
  • 举报
回复
非常感谢以上几位高手
老紫竹 2007-12-26
  • 打赏
  • 举报
回复
import java.io.*;

public class Ex {
public static void main(String[] args) {
Ex te = new Ex();
te.m(0);
}

void m(int i) throws ArithmeticException {
if (i == 0)
throw new ArithmeticException("被除数为0");
}
}
NicholasYJ 2007-12-26
  • 打赏
  • 举报
回复
void m(int i) throws ArithmeticException {
if(i == 0)
throw new ArithmeticException("被除数为0");
}
应该写在main函数的外面!

import java.io.*;

public class Ex {

public static void main(String[] args) {
Ex te = new Ex();
te.m(0);
}

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

zhaoyunf2005 2007-12-26
  • 打赏
  • 举报
回复
加上try catch语句和不加上两种情况都编译有问题,过不去。
zhaoyunf2005 2007-12-26
  • 打赏
  • 举报
回复
我检查了有的,语法应该没有问题。
notwizard 2007-12-26
  • 打赏
  • 举报
回复
你的main() method 没有 }

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧