[英]Why doesn't android studio always warn about calling methods which throw exceptions?
In some cases, android studio lets me call a method which throws an exception, without a try/catch. 在某些情况下,android studio让我调用一个引发异常的方法,而无需进行try / catch。 Why is that?
这是为什么? How can I enforce it to always warn me about it?
我该如何执行它以始终警告我呢?
Checked Exceptions are those that are inherited from the Exception
class, you will be warned Checked Exception是从
Exception
类继承的那些,将警告您
Unchecked Exceptions are inherited from RuntimeException
, calling a method that throws them will never show a compiler warning, but as the name suggest "just" mess it up at runtime ;-) 未检查的异常是从
RuntimeException
继承的,调用抛出异常的方法将永远不会显示编译器警告,但是顾名思义,“只是”在运行时将其弄乱了;-)
Here a short list of common exceptions, to help you weary restless searching fellows out ... 这里是一些常见例外的简短列表,以帮助您摆脱烦躁不安的搜索伙伴的烦恼...
The problem seems to be about checked/unchecked Exceptions, as @RubioRic and @Altoyyr said. 正如@RubioRic和@Altoyyr所说,问题似乎与检查/未检查的异常有关。 I didn't know about the difference.
我不知道区别。
For the exception you can handle, use 对于您可以处理的异常,请使用
try{
//your business logic
}catch(Exception e) {
//Err handling logic
}
For the exception you cannot handle, do not use an empty try/catch block, but use throws exception instead. 对于无法处理的异常,请不要使用空的try / catch块,而应使用throws异常。 Let the custom error handler you wrote to handle it from outer class etc.
让您编写的自定义错误处理程序从外部类等处理它。
//business logic which you know it will throw certain kind of exception
throw XException("your err descrption");
The last case is you don't even know what happened, please fix the code according to the exception/ err trace, it is a bug where business logic should handle well. 最后一种情况是您甚至不知道发生了什么,请根据异常/错误跟踪修复代码,这是业务逻辑应能很好处理的错误。
Please read about the error handling in java throught this link: http://www.onjava.com/pub/a/onjava/2003/11/19/exceptions.html 请通过此链接阅读有关Java中错误处理的信息: http ://www.onjava.com/pub/a/onjava/2003/11/19/exceptions.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.