java获取未报告的异常

nwlqm0z1  于 2021-06-02  发布在  Hadoop
关注(0)|答案(2)|浏览(399)
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会显示消息,但不会创建目录。

i1icjdpr

i1icjdpr1#

仅仅加上:

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

即: public void createDirectory(String path) throws Exception 你的方法?

qqrboqgw

qqrboqgw2#

如果要抛出异常,必须在方法签名中声明它。对你来说那就是

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
}

相关问题