调用抛出 FileNotFoundException 的方法

发布于 2024-12-06 17:55:39 字数 185 浏览 1 评论 0 原文

我很确定这是一个简单的问题,但我找不到直接的答案。如何调用抛出 FileNotFoundException 的方法?

这是我的方法:

private static void fallingBlocks() throws FileNotFoundException

I'm pretty sure this is an easy one but I could not find a straight forward answer. How do I call a method with a throws FileNotFoundException?

Here's my method:

private static void fallingBlocks() throws FileNotFoundException

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

审判长 2024-12-13 17:55:39

您调用它,然后声明您的方法也抛出它,或者捕获它:

public void foo() throws FileNotFoundException // Or e.g. throws IOException
{
    // Do stuff
    fallingBlocks();
}

或者:

public void foo()
{
    // Do stuff
    try
    {
        fallingBlocks();
    }
    catch (FileNotFoundException e)
    {
        // Handle the exception
    }
}

参见Java 语言规范第 11.2 节Java 异常教程 了解更多详细信息。

You call it, and either declare that your method throws it too, or catch it:

public void foo() throws FileNotFoundException // Or e.g. throws IOException
{
    // Do stuff
    fallingBlocks();
}

Or:

public void foo()
{
    // Do stuff
    try
    {
        fallingBlocks();
    }
    catch (FileNotFoundException e)
    {
        // Handle the exception
    }
}

See section 11.2 of the Java Language Specification or the Java Tutorial on Exceptions for more details.

旧人哭 2024-12-13 17:55:39

您只需像调用任何其他方法一样调用它,并确保

  1. 在调用方法中捕获并处理 FileNotFoundException 即可;
  2. 确保调用方法的 throws 列表中有 FileNotFoundException 或其超类。

You just call it as you would call any other method, and make sure that you either

  1. catch and handle FileNotFoundException in the calling method;
  2. make sure that the calling method has FileNotFoundException or a superclass thereof on its throws list.
神经暖 2024-12-13 17:55:39

您只需捕获异常或重新抛出它。了解异常

You simply catch the Exception or rethrow it. Read about exceptions.

ゃ懵逼小萝莉 2024-12-13 17:55:39

不确定我是否明白你的问题,只需调用该方法:

try {
    fallingBlocks();
} catch (FileNotFoundException e) {
    /* handle */
}

Not sure if I get your question, just call the method:

try {
    fallingBlocks();
} catch (FileNotFoundException e) {
    /* handle */
}
万劫不复 2024-12-13 17:55:39

是不是和调用普通方法一样。唯一的区别是您必须通过将异常包含在 try..catch 中或从调用方方法抛出相同的异常来处理异常。

try {
    // --- some logic
    fallingBlocks();
    // --- some other logic
} catch (FileNotFoundException e) {
    // --- exception handling
}

或者

public void myMethod() throws FileNotFoundException {
    // --- some logic
    fallingBlocks();
    // --- some other logic
}

Isn't it like calling a normal method. The only difference is you have to handle the exception either by surrounding it in try..catch or by throwing the same exception from the caller method.

try {
    // --- some logic
    fallingBlocks();
    // --- some other logic
} catch (FileNotFoundException e) {
    // --- exception handling
}

or

public void myMethod() throws FileNotFoundException {
    // --- some logic
    fallingBlocks();
    // --- some other logic
}
愛上了 2024-12-13 17:55:39

您也可以像任何其他方法一样调用它。然而该方法可能会失败。在这种情况下,该方法会抛出异常。应使用 try-catch 语句捕获此异常,因为它会中断程序流程。

You call it like any other method too. However the method might fail. In this case the method throws the exception. This exception should be caught with a try-catch statement as it interrupts your program flow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文