简体   繁体   English

为什么android studio不总是警告抛出异常的调用方法?

[英]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? 我该如何执行它以始终警告我呢?

Two kinds of exceptions 两种例外

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 ... 这里是一些常见例外的简短列表,以帮助您摆脱烦躁不安的搜索伙伴的烦恼...

UNCHECKED exceptions 未检查的异常

  • StackOverflowError StackOverflowError
  • RuntimeException RuntimeException
  • ArrayIndexOutOfBoundsException ArrayIndexOutOfBoundsException
  • ClassCastException ClassCastException
  • IllegalArgumentException IllegalArgumentException
  • IllegalStateException IllegalStateException
  • NullPointerException 空指针异常
  • NumberFormatException NumberFormatException
  • AssertionError 断言错误
  • ExceptionInInitializerError ExceptionInInitializerError
  • NoClassDefFoundError NoClassDefFoundError

CHECKED exceptions 检查异常

  • Exception 例外
  • ClassNotFoundException ClassNotFoundException
  • CloneNotSupportedException CloneNotSupportedException
  • FileNotFoundException FileNotFoundException
  • ParseException ParseException
  • InstantiationException InstantiationException
  • InterruptedException InterruptedException
  • IOException IOException
  • NoSuchFieldException NoSuchFieldException
  • NoSuchMethodException NoSuchMethodException

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.

相关问题 为什么Java没有警告= =“某事”? - Why doesn't Java warn about a == “something”? 为什么Android中View.class的所有方法总是抛出异常 - Why all methods of View.class in Android always throw a exception 模拟总是抛出异常,除非是特定方法 - Mockito-ing to always throw exceptions except on specific methods 为什么android studio会警告我有关为输入项创建通用数组的信息 - Why does android studio warn me about generic array creation for typed entry 为什么 Android Studio 会警告此活动处理程序代码中的处理程序引用泄漏? - Why does Android Studio warn about a handler reference leak in this Activity Handler code? 调用不会在'try'块中抛出异常的方法 - Calling methods which not throws exceptions in 'try' block 为什么 Queue 有抛出异常的方法而其他 Collection 的类没有? - Why does Queue has methods that throw exceptions while other Collection's classes don't? 为什么重写方法不能抛出比重写方法更广泛的异常? - Why can't overriding methods throw exceptions broader than the overridden method? Dagger不喜欢抛出异常的构造函数 - Dagger doesn't like constructors that throw exceptions 使用 mockito 测试抛出未捕获的自定义异常的方法 - Using mockito to test methods which throw uncaught custom exceptions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM