简体   繁体   English

Java出现未报告的异常

[英]Java getting unreported exception

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

Error 错误

error: unreported exception Exception; 错误:未报告的异常 must be caught or declared to be thrown 必须被抓住或宣布被抛出

If I remove try catch then code compiles and logger shows the message but directory is not created. 如果删除try catch,则代码将编译,记录器将显示该消息,但未创建目录。

Isn't it enough just to add: 仅添加以下内容还不够:

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

That is: public void createDirectory(String path) throws Exception to your method? 也就是说: public void createDirectory(String path) throws Exception方法public void createDirectory(String path) throws Exception吗?

If you want to throw a exception you have to declare it in your method signature. 如果要抛出异常,则必须在方法签名中声明它。 In your case that would be 在你的情况下

public void createDirectory(String path) throws Exception

Or you could catch it without throwing 或者你可以不扔就抓住它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM