gpt4 book ai didi

java - Java出现未报告的异常

转载 作者:行者123 更新时间:2023-12-02 21:11:56 25 4
gpt4 key购买 nike

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

错误

error: unreported exception Exception; must be caught or declared to be thrown



如果我删除try catch,则代码将编译,记录器将显示该消息,但未创建目录。

最佳答案

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

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
}

关于java - Java出现未报告的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40017310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com