简体   繁体   English

如何使Java编译器忽略错误/警告

[英]How to make java compiler ignore errors/warnings

So I have the following bit of code: 所以我有以下代码:

 public static Image getImage(String filepath, Class cl) {
    try {
        return ImageIO.read(cl.getResource(filepath));
    }
    catch (IOException e) {
        e.printStackTrace();
    }

    return null;    // Will never execute
}

It's a basic try-catch block. 这是一个基本的try-catch块。 If I am unable to read the image and return it, I immediately go into my catch block. 如果我无法读取图像并将其退还,我将立即进入catch块。 However, because my return is within the scope of the try block and not the entire function, my compiler issues an error when I try to compile and run because it sees that it's possible that I never hit a return statement. 但是,由于我的返回值在try块的范围内,而不是在整个函数的范围内,因此,当我尝试编译并运行时,编译器会发出错误消息,因为它认为有可能我从未命中过return语句。 Therefore, I've added the return null; 因此,我添加了return null; line to suppress this warning, but I'd rather have a neater way of doing this without putting code that will never run. 行以禁止显示此警告,但是我宁愿使用一种更整洁的方式来执行此操作,而不必放置永远不会运行的代码。 I've tried adding 我尝试添加

@SuppressWarnings("all")

To my code, but it still gives me an error. 对于我的代码,但这仍然给我一个错误。 Any ideas? 有任何想法吗? I feel like there should be a way to tell the compiler to ignore errors like this. 我觉得应该有一种方法告诉编译器忽略这样的错误。

Also, if it is of any use, I am using IntelliJ as my IDE. 另外,如果有什么用,我正在使用IntelliJ作为我的IDE。

I would suggest what @LuCio eagerly in the comments tried to say. 我想在评论中急于建议@LuCio说些什么。 Just don't catch the Exception and pass it upwards: 只是不要捕获异常并将其向上传递:

public static Image getImage(String filePath, Class<?> clazz) throws IOException {
    return ImageIO.read(clazz.getResource(filePath));
}

That way you have created an easy helper method. 这样,您就创建了一个简单的辅助方法。 If you would return null, you'd have to document that in JavaDoc and every caller will have to use a not-null assertion logic to then throw an error if it is null. 如果要返回null,则必须在JavaDoc中进行记录,并且每个调用者都必须使用not-null断言逻辑,如果错误为null,则抛出错误。

A try catch block does the same. 尝试捕获块的功能相同。 So instead of passing null upwards you just propagate the exception upwards. 因此,您无需向上传播null,而是向上传播异常。 You somewhere said that you want to assign the Image to a static field, so you can do that easily like this: 您在某处说过要将图像分配给静态字段,因此您可以像这样轻松地做到这一点:

static {
    try {
         MY_IMAGE = getImage("somepath", MyClass.class);
    } catch(IOException e){
        throw new IOError(e); // will kill the Vm with an error
    }
}

But maybe somewhere you have another action. 但也许在某个地方您还有其他动作。 Than to just kill the VM. 比只是杀死VM。 Maybe use a default image: 也许使用默认图像:

final Image image;
try {
    image = getImage("somepath", MyClass.class);
} catch(IOException e){
    e.printStacktrace();
    image = new SomeDefaultImage();
}

// do something with image

Which all in all is the way to go. 总而言之,这是要走的路。 You can't have a helper method to decide what to do when it fails. 您无法使用辅助方法来确定失败时的处理方法。 That should always be done by the calling code. 这应该始终由调用代码完成。

Ok so, I believe I was confusing the purpose of the catch block. 好的,我相信我混淆了catch块的用途。 Thank you to @Ben and @Zephyr and everybody else for your help. 感谢@Ben和@Zephyr以及其他所有人的帮助。 I will be amending my code to: 我将代码修改为:

public static Image getImage(String filepath, Class cl) {
    try {
        return ImageIO.read(cl.getResource("hello"));
    } catch (IOException e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
        throw new IOError(e);
    }
}

Edit: After some more discussions, and looking through other options other people have posted, I have updated my code above, which satisfies the compiler. 编辑:经过更多讨论,并查看其他人发布的其他选项,我更新了上面的代码,该代码满足了编译器的要求。 Note that replacing the line 请注意,替换行

throw new IOError(e)

with

System.exit(0);

does not fix the error because, as far as I know, the compiler cannot tell at compile time whether the program would end. 无法修复错误,因为据我所知,编译器无法在编译时告知程序是否将结束。 It would've been helpful to have a way of suppressing the warning, since we know that at runtime the program will always (or practically always) end, but alas @SuppressWarnings is of no use. 拥有一种抑制警告的方式将很有帮助,因为我们知道在运行时程序将始终(或实际上始终)结束,但是@SuppressWarnings毫无用处。

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

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