8 ms·
Java Exception Handling
- norswap 10y agoSee this article (http://norswap.com/checked-exceptions/ http://norswap.com/checked-exceptions/) on why exceptions are generally misused.
- the_arun 10y agoAre there guidelines for using exceptions in a correct way?
- jacques_chester 10y agoMy thinking would be: First, don't catch exceptions unless you really can handle them. Swallowing and emitting a log line is not handling an exception. Second, and this is a corollary, only catch the exception classes you can handle. The classic antipatterns of swallow-and-log or swallow-and-convert are very frustrating. Swallow-and-log means that you don't see the true state of the world upon exceptional conditions arising. It is particularly harmful for gutting the usefulness of acceptance and unit testing. Swallow-and-convert means that you deny the rest of the stack a chance to properly handle the exception. You also suppress the information which checked exceptions provide to consumers of your method, converting something that can be solved at coding time into runtime error lotteries. Edit: per the comments below, my meaning for "swallow-and-convert" is the practice of turning meaningful exceptions into `RuntimeException` because it's "too hard" to add exceptions to consuming method signatures.
- sorokod 10y agoThere are cases where 'swallow-and-convert' is a reasonable thing to do. This often comes up in multi layer architectures where an exception from a lower layer makes no sense to a higher level and some sort of semantic conversion/translation is needed.
- jacques_chester 10y agoAgreed, I overlook that case. I've done exactly that in Java and Ruby.
- Hermel 10y agoIn large, layered software projects, the best solution can be "catch and wrap". In that case, the wrapping exception provides information about what went wrong, and the wrapped exception says why. For example, a CouldNotLoadImageException might wrap a JPegDecodingException or a CouldNotConnectException, depending on what went wrong underneath.
- mlvljr 10y agoA book even! I linked it below, as a level 1 comment.
- wellpast 10y agoThis is flawed thinking. I'd say in many cases it is perfectly desired to either swallow or re-throw exceptions. As just one example, many business rules can be correctly implemented as best-effort and if some resource (network or service) is unavailable simply let the failure propogate, or log, or backoff and retry some other time. And most certainly but in fewer cases more robust handling is called for. Even if only 10% of exception handling is in this category, then try/catch is still a valuable tool. I find it really faulty to reason that "this feature is only used 'correctly' 10% of the time, therefore it's a failed feature."
- draw_down 10y agoThat's what I was thinking. Maybe once in a while I just want to catch a parsing error specifically (like when parsing a string into a number) and then other times I just want to hear when anything goes wrong with some operation. That's not necessarily a misuse of exception handling.
- gsmethells 10y agoThe diaper pattern is a well-know anti-pattern. But the idea that all exceptions are thus evil is a giant leap of logic that is hard to swallow.
- emsy 10y agoAt least in Java, exceptions are often very granular, when you actually don't care. Let's say you want to read a file. You may get an exception for malformed paths, permission errors, filesystem errors and so on. It's unlikely that I will recover from any of these errors during runtime. The only thing I really can do is log the error, inform the user and send a report.
- sgift 10y agoMy usual use case involves adding that file to a "retry later" queue. Just logging it and hoping for the best doesn't cut it if you really need to read the file. But maybe I'm an outlier. Most files I see have to be processed, either now or later, without any manual interaction.
- guard-of-terra 10y agoExceptions are maddengly, insanely efficient in some settings, such as making a server handling multiple requests at once. They allow you to confine errors on any single request to that request. Many languages/platforms suggest you to use multiprocessing for that. Also helps that servers usually have well-understood failure modes (DB error, data validation error, network failure) compared to other kinds of software (e.g. a missing file may lead to a whole investigation there) For e.g. desktop programming, exceptions are kinda less useful.
- orf 10y ago> They allow you to confine errors on any single request to that request I don't understand this, returning error codes has the same effect. In fact you can do that with most forms of error handling, short of segfaulting?
- sgift 10y agoThe problem usually is: What is the error code if your return type is int? Sure, you can define all negative ints as errors, or you provide a reference which is set when an error is encountered and so on, but that ends up being just another type of "very ugly". At the moment, I'm mildly intrigued by Rusts result types which force you from the start to handle "all cases". They strongly remind me of checked exceptions.
- kilink 10y ago> The problem usually is: What is the error code if your return type is int? Sure, you can define all negative ints as errors, or you provide a reference which is set when an error is encountered and so on, but that ends up being just another type of "very ugly". Many languages support multiple return values, algebraic data types, or box types, allowing you to return an error value out of band.
- sgift 10y agoWhat is the difference between an exception and an "out of band" return of an error value?
- johnwatson11218 10y agoThe pragmatic programmer book has a great quote about exceptions. Something about how once an exception is caught your stack starts to unwind and a new program is now running on your system. Can't find it right now. The worse production bugs I have seen involve complicated exception handlers that generated a second exception while trying to write to the filesystem or something. In that case some other exception handler now starts to execute further up the stack. It really is as if a new program is now compiled and installed on your production servers. A new program that has read/write access to the production db. A program that most likely was never executed in QA and has never been seen by any person. It is a bit hyperbolic but I have never seen an exception handler over four lines long that did not contain a bug. My attitude towards exceptions is like a fire alarm. If I hear a fire alarm it means one thing and I have one action to take. Any notion of providing different pitched fire alarms to encode information would be absurd and dangerous, that is how I view exception hierarchies. It is my understands that the Go language from google decided not to include this language feature. I have heard that Java's checked exceptions are now seen as an important, failed experiment.
- sgift 10y ago> I have heard that Java's checked exceptions are now seen as an important, failed experiment. Sadly, this is what people often say these days, I still think they provide far more value than unchecked exceptions. I really liked it when I didn't have to provide any exception signatures and didn't have to think about where to catch them. I didn't like it so much anymore when unchecked exceptions started crashing my code, because there were exceptions thrown at points I never expected them to be thrown. Either no exceptions or checked exceptions, but unchecked? My head :(
- silvestrov 10y agoThere are almost always the possibility of exceptions: you can get NullPointerException, OutOfMemoryError, ArrayIndexOutOfBoundsException, etc. There is a log of exceptions that derive from RuntimeException. You must always assume that code in the exception handler can fail unless you have written every single line and it allocates no memory at all.
- michaelfeathers 10y agoThe great thing about exceptions is that they allow you to put arbitrary distance between the point of detection and the point of handling. The problem with exceptions is that they allow you to put arbitrary distance between the point of detection and the point of handling.
- noodnik 10y agoMost of the time, checked exceptions remain an unused language feature, because: A.) String to Integer with null handling for HTTP GET/POST transformation isn't worth the effort, because you're users are drunk idiots, and they won't pay attention to your carefully stratified error messages anyway. So just tell them to sober up, straighten out, and fly right, and barf a hideous, uninformative stack trace at them to scare them off until they go away, and come back in the morning. B.) Developers are so rushed to just get shit done that NullPointerExceptions, NumberFormatExceptions, ArrayIndexOutOfBoundsExceptions and so forth, are useful enough, and probably don't need much more differentiation anyway. As middleware developers, we're really not dealing with anything too exotic, and so we mostly don't need to re-interpret things like sensor or transducer state, or spin dial knob rotation angle, or leaf spring load, or magnetic flux. So, just because 90% of server side web developers don't use it, doesn't mean it's a failure. It just means that interpreting discete character data from noodniks on the other side of a keyboard and pointing device, behind a TCP/IP connection can only get so complicated.
- rodionos 10y agoThe volume of logging in production systems is such is that it is impractical to act on individual exceptions. You would typically send log events to a centralized logging service or have an agent-based code monitor logs locally for well know sequences or error codes. And that's where Java exceptions can be improved - a built-in facility to assign error codes so that downstream tools can handle exceptions based on a rule book which takes application-specific error codes into account.
- ivan_gammel 10y agoIf you don't want to use regular expressions for matching exceptions, you can simply calculate hash code of exception class name and, possibly, of it's cause and message and pass it downstream.
- cbsmith 10y agoBetter yet have monitors/counters...
- jdiscar 10y agoI think this is fine. Java is verbose as it is, it would be even worse if Exceptions were handled "as intended" and every Exception type was treated individually. Log and move on can be appropriate in some cases. And even if 80% of the time you're ignoring all Exceptions, the 20% of the time you have specific code could be very, very important.
- jcrites 10y agoJava exception handling is very successful, and does exactly what I want as a system designer. The article objects to the fact that applications handle many exceptions by logging them and giving up. What's wrong with that? That's perfect. For the class of exceptions that I can just log and give up, that's often what I want to do. If I had an open TCP socket to a remote host, then I can't "recover" from an exception indicating that the connection was broken. Instead, I just want to tear down everything related to that connection and carry on. Exceptions can rarely be recovered from at the level of abstraction where the exception occurred. For example, I can't repair a socket that's been broken. I cannot retry the request at that level - it might not be idempotent; the socket processing code can't know. The connection pool doesn't know. The HTTP client might or might not know. However, I can recover from the exception at a higher level -- at the level of the code that understands what the task is, it might retry the top level processing job, which might invoke a call on a service client, which will invoke a call on an HTTP client, which will invoke a call on an HTTP connection pool to open a new TCP socket as needed. This is exactly the pattern that we see commonly in Java applications: a higher level module calls a lower level one, and then variously logs exceptions and tries again (possibly with exponential backoff over multiple attempts), or logs them and give up, or does something else. You don't really "repair" or "recover" from exceptions. You mitigate them by handling them and carrying on with the job. To achieve this, you unwind up to the level where it makes sense to retry or abort a task. When you retry, you retry from the right level of abstraction needed to start over. The extremely valuable property that exceptions have, that they give you, is confidence about what specifically went wrong. They constrain it. If I try to interact with a socket and I get an IO exception, then I know the error is constrained specifically to that socket. The rest of the program is working fine, and I can either try to recover from that error (sometimes possible), or close the connection and open a new one, or give up processing the task. Exceptions are also valuable because you don't have to add error-handling logic at each layer in an application. Many layers can be ignorant or agnostic of the exceptions that will propagate through them, and that's a good thing because it reduces coupling. These layers might be inserted between the high-level logic that understands task processing, and that will make the judgment call about retry/backoff/give up, and the specific code that can fail and throw an exception. I don't want all layers of code to have to care about exceptions that might pass through them; they can't and shouldn't do anything about it. It is valuable to be able to build abstractions and build code that exceptions pass through, without handling them. You might add logic to handle them later, or you might do so at a higher level, or a lower level.
- johnwatson11218 10y agoThe best use of exceptions in java that I have seen in production is when an exception is used to roll back a database transaction or when the web layer is able to catch all exceptions, log them, and then redirect the end user to a nice looking page telling something went wrong. Another anti-pattern is subclassing Exceptions to have things like "UserNameInUse" exceptions during a sign up path to indicate that a selected username is not unique in the db. This becomes a poor man's message passing implementation. Using an exception to signal that is very inefficient as the exception has the entire stack trace as part of its data.
- barrkel 10y agoUsing an exception to signal a semantic error is only expensive if the language implementer has made that expensive. Technically, all the exception throw needs do is pop stack frames until it finds a handler. Getting a stack trace is optional, but keeping track of the return addresses (pushed by the CPU's call instruction) is sufficient to materialize that on demand. An array of integers isn't expensive either. I don't believe transactional programs using exceptions for rollback is an anti-pattern. It's not the best pattern - a transactional language, with transactional data structures, is much harder to get wrong - but it's not worst, not by a long shot.
- johnwatson11218 10y agosorry - I didn't mean to indicate that using transactions to signal a roll back is an anit-pattern. I started out that comment listing good things I had see exceptions used for. Then that example of the userNameInUse exception came to mind. I realize my choice of words made that less than clear.
- pacala 10y agoThere are two kinds of exceptions. A bug in the data or a bug on the code. The only action to take is to pass detailed info to the user and/or the coder. Fix the bug and try again beats any precanned exception recovery code one might waste time implementing.
- sgift 10y agoSee jcrites comment here https://news.ycombinator.com/item?id=11842726 https://news.ycombinator.com/item?id=11842726 for an example where neither the code nor the data has a bug. Broken sockets are simply a part of distributed systems the code has to deal with. (If you feel that "the network should be reliable" is a valid answer, see: https://en.wikipedia.org/wiki/Fallacies_of_distributed_computing https://en.wikipedia.org/wiki/Fallacies_of_distributed_compu...)
- pacala 10y agoWe should classify exceptions by the _action_ required to fix them, not by the circumstances in which they appeared. "DivideByZeroException" is useless, "User error: 31st of February is not a date" or "Internal error: count is zero" are a whole lot more actionable. Java's menagerie of exceptions is an anti-pattern. I grant you that there is a third class of exceptions, "remote unavailable", for which the error handling is to retry X times, then promote to "system broken" and let the user retry later. For all intents and purposes, "system broken" is a better name for "code error", covering also cases where the DevOps team should fix the hardware or dependencies on third party systems. [Configs are funnily written code]. Often times the "retry X times" logic is wrapped nicely in one's RPC infrastructure of choice, so what the app developer has to deal with is either "user error" or "system broken".
- barrkel 10y agoThat's not right. Exceptions usually aren't bugs. See my comment here: https://news.ycombinator.com/item?id=11843464 https://news.ycombinator.com/item?id=11843464
- cbsmith 10y agoThose aren't exceptions, those are bugs. Exceptions happen all the time because of operational errors.
- golergka 10y agoA lot of opinions about error handling in this thread, but I don't see the main thing mentioned: difference in requirements of different projects and subsequent difference in technical strategies. Some applications need to fail fast, fail hard, and restart automatically. Some applications need to present any failure or error condition to the user and give him power to decide. Some applications need to be restored to some sensible state at all cost. Needless to say, all possible error handling strategies, from C return codes to Java checked exceptions, have their fair use cases; discussing which one is better and which one is worse without specific project in mind, at least as an example, is completely pointless.
- mlvljr 10y agoExceptions! There is a book solely dedicated to the topic: http://www.amazon.com/dp/0131008528/?tag=stackoverfl08-20 http://www.amazon.com/dp/0131008528/?tag=stackoverfl08-20 Had it mentioned (together with another only-one, but fpor ASP) in an http://programmers.stackexchange.com/questions/14831/how-to-teach-exception-handling-for-new-programmers http://programmers.stackexchange.com/questions/14831/how-to-... answer, which the proud professionals then dewnvoted and eventually deleted :)
- barrkel 10y agoThere's a lot of misconceptions around errors, and this article isn't free of them either. Java's checked exceptions feature derives almost entirely from a misconception around the nature of errors: that errors should, as a rule, be handled. Only a small subset of errors should ever be handled, usually quite close to the site of the error - an unwound stack seldom has enough information to anything useful other than log, inform client, or abort. If the error is to propagate, there's little win in forcing the whole call tree to need to know the details, since the nature of the error conveys less and less information the further away it is from its occurrence - context is necessarily lost through abstraction. Errors can be grouped into 3 categories: programming mistake, semantic error and non-deterministic error. The first category are bugs that are discovered through sanity checking and defensive programming - conditional code that is only invoked in the case of a mistake and is always avoidable. Depending on the environment, a whole program abort or restart might be a valid response; a localized response (like handling an exception) isn't going to be useful. Fall back to the event loop / request-response loop, log and continue usually suffices for non-critical apps. Checked exceptions are not a good idea, but that's OK - Java uses unchecked exceptions for these, even if not all libraries do the same. The second category are semantic errors, typically where a client or end user has tried to do something invalid, and something like exceptions come in handy to unwind a partial operation, a bit like rolling back a transaction. Starting out with optimism but rolling back once a problem has been discovered isn't an unusual or technically flawed approach; exception handling is perhaps an error-prone way to do it, but it is a way. And it's a case where localised exception handling again isn't a good idea. This is a problem area in Java, as far too many applications use checked exceptions here. The third category is non-deterministic errors: something outside the world of the program didn't conform to expectations. The other side of a network socket disappeared; a file was deleted; a device was disconnected; etc. You can't detect the error before attempting the operation, and the resulting error is, in a way, a type of return value - unexpected information - from the attempt. Fixing the problem is almost certainly only likely to be successful if it's local (hacks like lazy creation of files, automatic reconnects of sockets, etc. aside). Propagating the error is fine if it can't be fixed locally - most code isn't written to have alternative strategies for these scenarios - but forcing callers to handle this specific type of error - that's not terribly useful. Again, checked exceptions - specifically, typed checked exceptions - not giving up the benefit they promised. The fact that something might fail in a non-deterministic way is useful information at the API level, but it also violates modularity. The "fix" in Java is for every module to throw its own hierarchy of checked exceptions, which, of course, is no fix at all: now you have even less prima facie information, and useless exception handlers and exception specifications proliferate until the meaning behind them is lost. Other languages that use option types to propagate errors work fine for the third category; they usually have a different scheme for the first category; but on the second category, they are usually silent, and that's a bad thing in my opinion. Something exception-like is very valuable in the absence of strong tools for transactional modifications of program state. You shouldn't take away exceptions without providing a solid way of throwing away partial work. Erlang - tear down the process - that's OK; functional languages - immutable state necessitating immutable, persistent data structures - that's OK; something like Rust - well, it's going to need panic for more than just the first category.