最近在学习马士兵的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 错误

解决方案 »

  1.   

    你的main() method 没有 }
      

  2.   

    加上try catch语句和不加上两种情况都编译有问题,过不去。
      

  3.   

    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"); 
        } 
      

  4.   

    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");
      }
    }