Why does Java have both checked and unchecked exceptions?

Possible duplicate:
When to select marked and unverified exceptions

Why does Java as a language have both checked and unchecked exclusion flags. What purpose do they serve?

Note I do not ask when I should use them or how to encode them, but what do they add to the language.

+4
source share
6 answers

The theory of checked exceptions is simple.

When designing an interface, consider the exceptional cases that can and will happen with the normal state of the method call. Declare these exceptions in your interface, as the programmer will have to deal with them directly.

For example, a bank account withdrawal method may throw an OverdraftException, which is the expected exception - the output may fail due to overdraft, but this type of failure can be handled differently using the client code (you can decide to completely refuse another, can decide to apply a huge fine and allow the recording of a negative balance, another may decide that their client is allowed to draw from another account).

However, runtime exceptions should have been programming errors that should not have been handled directly - for example, NullPointerExceptions, which occur only if the methods accept invalid arguments or do not check for such cases directly.

This is a good theory. However, Java ruined the implementation of Exceptions, and it threw the book of this theory out of the window.

There are two cases that I will illustrate where Java messed up its implementation of Exception. These are IOException and SQLException.

An IOException occurs anytime, anywhere, when a thread in a Java IO library gets tangled up. However, this is a proven exception. But as a rule, you can do nothing except the journal, that an error occurs - if you just write to the console, what can you reasonably expect if you suddenly get an IOException when writing?

But there is more.

An IOException also hides things like file exceptions and network exceptions. They may be subclasses of IOException floating for this, but this is still an exception. If your letter to an external file fails, you cannot really do much if your network connection is disconnected, the same.

SQLException is the same. Exception names should indicate what happened when they are called. SQLException is not. A SQLException is thrown every time there is any possible number of errors when working with a database - the MOST THAT YOU DO NOT NEED TO DO WITH SQL.

Therefore, programmers are usually annoyed by exception handling and let Eclipse (or any other IDE that they use) create blocks like this:

try { thisMethodThrowsACheckedExceptionButIDontCare(); } catch(Exception e) { e.printStackTrace(); } 

However, with RuntimeExceptions, they intentionally fail and are eventually processed by the JVM or container level. This is a good thing - it causes errors to be displayed, and then you have to fix the code directly, and not ignore the exception - you can still just print the stack trace (I hope that it will write it, and not print to the console directly), but then there will be an exception handler that you were forced to write because of the real problem - not because the method said that it could throw an exception, but what it did.

Spring uses a DataAccessException to throw SQLExceptions, so you don't need to treat them as a checked exception. As a result, the code becomes much cleaner - if you expect a DataAccessException, you can handle it, but most of the time you allow it to propagate and register as an error, because your SQL should be debugged by the time your application is released, which means that DataAccessException is probably is a hardware problem that you cannot solve - DataAccessException is a much more meaningful name than SQLException, because it shows that access to the data failed - not that your SQL query was to blame.

+13
source

They add a differentiation between the errors that the library developer feels and those that, in their opinion, should not be handled by the programmer.

For example, it might be reasonable for a program to handle bad user input, but if something went wrong with the underlying OS and the threads start dying for no reason, this is not what the program should expect from processing.

+5
source

Personally, I believe that checked exceptions were a bug in Java.

Aside, denoting both checked and unchecked exceptions, allows the library to distinguish between recoverable and unrecoverable errors. Due to the fact that all recoverable errors throw checked exceptions, the library / language may cause the developer to process edges that they might otherwise use.

The big problem with this:

 try{ myCode(); }catch(Exception e){ //Do nothing } 

In addition, in most cases, it is best to simply raise your hands and pass an exception when this happens. Forcing checked exceptions to be declared, a method that doesn’t really care if an error occurs ends with the presence of dependencies (in terms of compatibility, as well as the smell of the code and others), this really should not.

+5
source

Verified and unchecked exceptions cause a bit of religious argument - Java fell on one side of the fence, and C # on the other.

In checked exceptions, Java should be used when the calling code can recover from an error, when exceptions are thrown without exception, when there is a critical error (perhaps an exception is a pun - NullPointerException ) that the calling code is unlikely to recover.

Personally, I like that both of them are accessible to me, as a rule, preferring checked exceptions, because they allow me to make the calling code cope with an error situation that the developer could ignore (although the infamous empty blocking block blocks this).

+4
source

I think Sun initially thought it would be a good idea, because the programmer is forced to handle the exception. However, many years later, almost everyone agrees that this is a bad, unnecessary addition.

One of the main problems (except for clutter code) is that they abstract the leak from the lower layers to higher levels (for example, remote rmi exceptions).

+3
source

I don’t think that something is completely wrong with checked exceptions ... but they actually tend to suck in practice, because (especially early) library developers use them excessively.

Also the requirement to "catch or declare" is NOT suitable with interfaces.

My thoughts are based on the following: “A thing that goes wrong” occurs in two main ways: recoverable and unrecoverable ... Ergo: business exceptions and system errors.

For example: what do you (the library developer) expect from me (the application programmer) to recover from a crash when calling SomeStream.close ()? Yes, I definitely should be aware that something went horribly wrong, but actually my only option is to stop the program / request / process / thread that worked on it. I cannot be reasonably expected to even try to recover from the situation ... Ergo: This is an infallible error, and therefore I should not be forced to write a lot of highly reliable template locks that do not cope with the problem at every level (potentially very deep) callstack. Therefore, I believe that it would be better if "caught all" checked exceptions, such as IOException, were never invented ... CloseException extends UncheckedIOException would be more appropriate, IMHO.

Also, if I had a time machine, I would return on time and plead with the Java gods:

  • throwable interface
    • abstract class exception
      • abstract class CheckedException
      • abstract class UncheckedException
    • Class error

Also: I would like to see the annotation of the @FaultBarrier class, which will force the compiler to enforce: all exceptions (especially unchecked ones) must be caught or explicitly selected. The most terrible piece of system I have ever worked with has been riddled with raw RuntimeException metaphors; This is enough to make you cry.

Greetings. Whale.

+1
source

All Articles