How can I find out what exceptions can be selected from a method?

How to find out which exceptions can be selected from a method call?

+3
source share
6 answers

Look at the throwsmethod signature suggestion to see which “checked” exceptions can be thrown. Method subscribers will have to distribute this information in their own proposal throwsor handle the exception.

, RuntimeException Error . , . , "" , RuntimeException. .

API- , , JavaDoc, . .

Error . - , .

+6

"".

, .

, , "".

+2

( Javadoc). , .

.

- , java.lang.Exception ( Exception), java.lang.RuntimeException . - , Throwable ( Throwable), Exception, RuntimeException . , ...

+2

- , RuntimeException, , . , .

- RuntimeException - .

, , javadoc.

, , Throwable. API-, API.

+2

"" .

. , , , .

, , , , , , , Java .

: , , .

+1

. , throws , ( ) .

. ( throws , . throws .)

The only way to get a more definitive answer is to use some (hypothetical) source code or a byte code analyzer. For example, you can determine that certain exceptions are never thrown or propagated by a method, simply by checking that there are no instances in the complete code base new SomeException(...). But for some exceptions (for example, NullPointerException and OutOfMemoryException) this analysis would be intractable, except in very limited cases.

+1
source

Source: https://habr.com/ru/post/1727824/


All Articles