Java获得未报告的异常

时间:2016-10-13 09:34:09

标签: java hadoop exception exception-handling

    public void createDirectory(String path) {
        try {
            shellSupport.executeCommand("hadoop fs -mkdir "+path);
            logger.info("Directory "+path+" created successfully");
        } catch(Exception exc) {
            throw exc;
        }
    }

错误

  

错误:未报告的异常异常;必须被抓住或宣布   被抛出

如果我删除了try catch,那么代码编译和logger会显示消息,但是不会创建目录。

2 个答案:

答案 0 :(得分:0)

仅仅添加:




  public void createDirectory(String path)抛出异常{
试试{
 shellSupport.executeCommand(“hadoop fs -mkdir”+ path);
 logger.info(“目录”+路径+“已成功创建”);
 } catch(Exception exc){
扔掉;
 }
}
  




即: public void createDirectory(String path)抛出异常到你的方法

&#XA?;

答案 1 :(得分:0)

如果要抛出异常,则必须在方法签名中声明它。在您的情况下将是

public void createDirectory(String path) throws Exception

或者你可以在没有投掷的情况下抓住它

try {
    shellSupport.executeCommand("hadoop fs -mkdir "+path);
    logger.info("Directory "+path+" created successfully");
} catch(Exception exc) {
    exc.printStackTrace();
    // handle your exception
}