Lam*_*bda 1 java exception-handling
坦率地说,我只是不明白我的导师要求我在这里做什么.我尝试过使用"try-catch"块,并在方法签名中抛出Exception.我读过关于已检查和未检查的异常.我相信这会被投票或关闭,但有人可以在这里扔我一块骨头吗?我的导师说明如下:
"纠正它,以便编译."
class Exception3{
    public static void main(String[] args){         
    if (Integer.parseInt(args[0]) == 0)             
        throw new Exception("Invalid Command Line Argument");     
     } 
}
很明显,它正在抛出RuntimeException.更具体地说,是一个ArrayIndexOutOfBoundsException.我知道异常的原因是因为数组是空的,所以引用的索引不存在.我的意思是,在技术上我可以抹去if(Integer.parseInt(args[0]) == 0),并throw new Exception("Invalid Command Line Argument");与更换System.out.println("It compiles now");
有任何想法吗?
public static void main(String[] args) throws Exception{         
    if (Integer.parseInt(args[0]) == 0)             
        throw new Exception("Invalid Command Line Argument");     
     } 
你的方法抛出Exception,所以方法声明应该指定它可以抛出Exception.
按照java教程
已检查的例外情况受Catch或Specify Requirement的约束.除Error,RuntimeException及其子类指示的异常外,所有异常都是经过检查的异常.