What is static exception checking and dynamic exception checking?

Someone mentioned on the online forum that Java static exception checking is small and causes scalability and versioning issues.

I realized that changing a class function throws a new exception, and breaking client code can be attributed to a version issue. But I'm not sure.

  • What is the scalability issue with static exception checking?

  • What is dynamic exception checking?

+4
source share
2 answers

Java checked exceptions can cause scalability issues when programmers cannot throw exceptions suitable for the method. (See Effective Java, paragraph 43: exception exceptions, corresponding abstractions).

Lazy programmers tend to add exceptions to the throws their method, not considering whether they are appropriate. As a result, methods collect more and more exceptions as you look higher and higher in the application architecture. Top-level methods may throw ten or more exceptions if you are not careful.

For example, in a method designed to encrypt data, many low-level exceptions can be thrown (for example, IOException , NoSuchAlgorithmException , KeyNotFoundException ...). There is no need to expose them to an API user. Instead, an EncryptionFailed exception may be thrown with the gory data stored in the cause field of the exception.


I assume that “checking for dynamic exceptions” can refer to catching Throwable or Exception (one of the root classes) and dynamically decide whether to handle the exception based on some external stimulus. For instance:

 List<Class<?>> exceptionTypes = new ArrayList<>(); exceptionTypes.add(IllegalArgumentException.class); exceptionTypes.add(IOException.class); try { // do something } catch (Exception e) { if (exceptionTypes.contains(e.getClass())) { e.printStackTrace(); } } 

Note: the above example is only an illustration. But imagine an IDE that runs code on behalf of a user and allows them to select the exception classes that they want to catch.

+3
source

I believe that checking for dynamic exceptions is just another name for runtime exceptions.

Although I believe in static exception checking, the Java static exception checking method does have problems with interface implementations.

Say we have an IParser . What relevant exceptions can its parse() method raise? Maybe a SyntaxErrorException . Now we want ServerResponseParserFromURL be used this way:

 class ServerResponseParserFromURL implements IParser { .... }; .... try { IParser parser = new ServerResponseParser(new URL("http://example.com/test.htm")); parser.parse(); .... } 

Here's the problem - an IParser implementation may fire, say, a NoRouteToHostException . Thus, we cannot use a beautiful and concise form and need to return to a long form:

 class ServerResponseParser implements IParser { .... }; .... try { String response = getServerResponse(new URL("http://example.com/test.htm")); IParser parser = new ServerResponseParser(response); parser.parse(); .... } 

As you can see, in practice, our IParser is an IStringParser , not a common IParser .

Although this may be acceptable in this particular case, it complicates the situation when we want to provide IParser as one of the arguments to f . Say, in order to be able to analyze the response of the server identified by the url, we cannot just pass the ServerResponseParser object and url - we need to find a way to pass the getServerResponse method f .

0
source

All Articles