java 返回值的静态方法可以抛出异常吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11312518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can static methods that return a value throw an exception?
提问by user1154644
I have a static method that returns a String, but in the event that the string that is passed in does not match one of several words, I want to throw an exception. The code below is just a sample of what I am trying to do, but I keep getting "non static variable this cannot be referenced from a static context" message on the line where I throw the exception. Basically, the return value from getMsg has to be valid, or the program cannot proceed, so I need a way to catch this.
我有一个返回字符串的静态方法,但是如果传入的字符串与几个单词之一不匹配,我想抛出异常。下面的代码只是我尝试做的一个示例,但我在抛出异常的行上不断收到“无法从静态上下文中引用的非静态变量”消息。基本上,getMsg 的返回值必须是有效的,否则程序无法继续,所以我需要一种方法来捕获它。
public static String getMsg(String input) throws UnknownInputException{
if (input.equals("A")){
return "key for A";
}
throw new UnknownInputException("Some Message");
return "unknownInput";
采纳答案by dnet
The problem is caused by the fact, that UnknownInputException
is probably a nested class, and if you instantiate it with the new
operator, as a nested class, it should have access to a "parent" object - which doesn't exist since the class was instantiated in a static context. For more information about this, see Static method returning inner class.
问题是由以下事实引起的,这UnknownInputException
可能是一个嵌套类,如果您使用new
运算符将其实例化,作为嵌套类,它应该可以访问“父”对象 - 由于该类已实例化,该对象不存在在静态上下文中。有关这方面的更多信息,请参阅返回内部类的静态方法。
A possible solution would be to declare UnknownInputException
as static
like this:
一种可能的解决办法是宣布UnknownInputException
为static
这样的:
private static class UnknownInputException extends Exception { ... }
Of course, you won't be able to access any instance (non-static) methods and/or fields from this class, but that might not be an issue in your case (especially in case of an Exception class).
当然,您将无法访问此类中的任何实例(非静态)方法和/或字段,但这在您的情况下可能不是问题(尤其是在 Exception 类的情况下)。
Also, return
ing value after the throw
line is unnecessary, as execution will never reach that line.
此外,行return
后的 ing 值throw
是不必要的,因为执行永远不会到达该行。
回答by user1154644
The UnknownInputException is an inner class. Once I made it static, the code compiled fine. Thanks for the help guys.
UnknownInputException 是一个内部类。一旦我将其设为静态,代码就可以正常编译。谢谢你们的帮助。
回答by Michael Besteck
The variable thisis not noted in the given example code, so it can not cause an error.
给定的示例代码中未注明变量this,因此不会导致错误。
The code return "unknownInput";
is redundant since never executed.
代码return "unknownInput";
是多余的,因为从未执行过。
There must be another static method in which this
is used, that causes the error.
必须使用另一种静态方法this
,这会导致错误。
回答by Mark Bramnik
Its perfectly legal in java to throw exceptions from static methods. However the code you've presented here can't be even compiled :) So provide all the code.
在java中从静态方法抛出异常是完全合法的。但是,您在此处提供的代码甚至无法编译:) 所以请提供所有代码。
The error you get here just says that you're using non static data fields defined on class from the static method. The static method doesn't belong to any instance, but the data field does...
你在这里得到的错误只是说你正在使用静态方法在类上定义的非静态数据字段。静态方法不属于任何实例,但数据字段...
回答by user691197
Firstly, the return "unknownInput";
line will never be executed. Is it not showing a dead code warning there ?
首先,该return "unknownInput";
行永远不会被执行。那里没有显示死代码警告吗?
And your method doesn't have any 'this' reference, are you sure it complains about this method ?
而且您的方法没有任何“this”引用,您确定它抱怨这种方法吗?