How to handle exception in Java?

Would replacing C-style error codes or Java-style exceptions with Lisp-style restarts improve the quality of software?

  • Most languages and libraries have a couple of ways of reporting when something goes wrong. Some use Java's exception structure and others simply have function which return unusual values when they go wrong. The problem with both of these approaches is that the calling program is left to deal with the error. If a calling program is complex and calls a large number of functions then it is possible that there will so many potential errors being returned that it's impossible or impractical to handle them all. I think this is why useless error messages such as "an error occurred" or "connect to the internet" when you are already connected show up in a lot of software. Common Lisp had a different approach to these. Although it offered exceptions it also offered restarts. This means that the underlying program would provide not just a warning of an error but a mechanism, or multiple mechanisms, for dealing with it or working around it. The caller can choose to use one of these, provide its own, or to leave the choice up to the higher level program or even the user if the choice was left unresolved (although this exposed the user to the debug interface which would be undesirable in modern software). Why have so many languages adopted exceptions and signals but not restarts? Would adding them to a mainstream language like Java or C# improve the way errors are dealt with in complex software?

  • Answer:

    The problem with exceptions is that they introduce a new control flow, complicating and potentially obscuring the program design This can impact the designer, developer, maintainer, tester, and tech support. Java and C++ consider exceptions to be true unrecoverable errors. Best practices discourage the use of exceptions as an alternate control flow, e.g. to short-circuit the stack when a non-error result is reached. I think you could make an interesting argument for a Lisp-style exception feature. I don't think it will ever be bolted on to a mature language which already has unrecoverable exception support and lots of existing code.

Sean Leary at Quora Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.