19

If we refer to exceptions as bugs, why not just call it a bug in the first place instead of an exception?

If in the code it's called exception and as soon as it occurs it's called a bug. Then why not call it a bug in the first place?

Thank you for any answer or comment.

5
  • I hope the technical details I mention help clarify the distinction of an exception vs a bug. Great questionn BTW, +1 Nov 5, 2011 at 2:07
  • semantics
    – user7519
    Nov 8, 2011 at 22:47
  • 1
    I actually don't know how you could confuse them, as they are very different things. Exceptions are cases handled by the code, which indicate some kind of error. Typically, errors of a vague kind, not exactly explainable, and such. - While bugs ... a bug is by definition unhandled by code itself. They are not even errors, they indicate lack of logic where it shouldn't be.
    – tvCa
    Dec 9, 2014 at 20:10
  • 1
    @NiklasRtz: Why the huge bounty? Tons of people would have answered regardless. Dec 10, 2014 at 15:58
  • @ThePopMachine Because I like huge bounties for questions other people might find interesting. I'm reqrding my "popular" questions and answer as much a I can. I've had much help from the good and fun questions and answers. I'm also preparing a new question about error handling and error codes for this programmers..that is not specific how to write code but how to handle errors in a limited and hopefully standardized way for exmple for webapps that can return a limited number of error codes, how deafults are good and naming of code parts. Dec 10, 2014 at 19:47

10 Answers 10

94
+200

Well, it's pretty simple: not all exceptions are bugs (and similarly, not all bugs manifest themselves as exceptions).

As example of an exception that's not a bug, if you're reading a file from a USB drive and someone yanks the drive out of the socket. That's going to raise an exception (in most languages that support exceptions, that is). But it's not a bug in the code.

Conversely, a bug might manifest itself as a calculation error or something. You still get an answer, it's just not the right one.

Having said that, an exception that makes its way all the way to the top of the stack likely is a bug. In my USB example above, you should be able to catch that exception and present a nice error to the user saying "We couldn't read from the file because it's no longer connected." or something. If you just present them with an IOException and some funky error code, then that's a bug. But the exception itself is not.

3
  • 1
    You are correct even when I look at how I do it: if a method fails getting the name of the nearest city (Los Angeles), it catches an exception and returns the name of the larger administrative area (eg California) but since it applies to any coordinate, a place with no close city is not a bug, it's an exception. Do you agree? Nov 5, 2011 at 3:20
  • 1
    @Nicke: Yep, I'd agree with that. Nov 5, 2011 at 6:00
  • 1
    Presenting an IOException and an error code is not always a bug. It's a diagnostic. I often do that for personal scripts, where failure means I just put in incorrect arguments. Dec 10, 2014 at 5:46
23

Plain and simple, an exception is not (always) a bug!

An exception is thrown (or should be) when something exceptional happens. If there is a problem with my hard drive and a file cannot be written, that is not a bug. That is a failure of the hardware.

A bug is generally a result of bad programming. If an application does something that is not expected as a result of a programming error, that is a bug.

5
  • 1
    Heh, we answered at almost the same time, and with basically the same example as well :-) Nov 5, 2011 at 0:57
  • 5
    @DeanHarding Great minds think alike, yes? :D
    – user7007
    Nov 5, 2011 at 1:01
  • 1
    While I agree with your first sentence, I must disagree with your last sentence. The first computer bug (though apocryphal) was, in fact, a moth trapped between the points of a relay. How is a malfunctioning hard drive any different? Nov 5, 2011 at 1:18
  • 1
    @ScottWhitlock I guess "bug" has more than one definition. I always assumed it to mean errors as caused by humans: en.wikipedia.org/wiki/Software_bug
    – user7007
    Nov 5, 2011 at 1:32
  • 1
    @ScottWhitlock: and supposedly programmers would say "not my fault, must be a bug", which backfired as "bug" came to mean a software fault. Today, a hardware failure would not be called a bug, although a bug could result in hardware failure.
    – jmoreno
    Dec 9, 2014 at 6:04
21

They are not the same thing.

A bug is unintended behavior of a piece of software: the software doesn't do what it is supposed to do. Bugs can live at all levels of software development, ranging from plain old typos through logical errors up to inadequate functional specs.

An exception, by contrast, can refer to either an unusual condition of a program, deviating from normal operation, or, more specifically, to the language construct used to signal and handle such conditions.

The fact that an exception occurs can be a sign of a bug, but often it isn't. For example, an application that is supposed to download a document from a URL and process it locally might throw an exception when the remote server is down: the application is deviating from normal operation (it cannot download and process the document), but if it handles the exception properly and recovers, then there is no bug.

Conversely, the presence of a bug doesn't necessarily manifest itself as an exception. An application might silently discard data you enter instead of storing it in its database; no exception gets thrown, but it is still a bug.

2
  • +1 for defining your terms. In general, people should do that more often!
    – yfeldblum
    Jan 7, 2012 at 21:56
  • This is definitely the clearest answer. very clear and concise. Great job!
    – Michael
    Dec 9, 2014 at 21:13
5

Exceptions and bugs are not at all related. Sure, sometimes you throw an exception and it means a bug. But sometimes it just means an exceptional, unusual circumstance, which is not necessarily a bug in the program at all. Especially in an exception-happy language like Java, where every standard operation and it's dog throws about five different exceptions- for example, opening file failed, reading file failed, etc.

3

Exceptions are not always bug related. Think of it as something that could go wrong with what you are doing.

An example that comes to mind is InetAddress.getByName() which is used to resolve a domain name. If something happens and an UnknownHostException is thrown its not really a code issue.

2

A software bug is the common term used to describe an error, flaw, mistake, failure, or fault in a computer program or system that produces an incorrect or unexpected result, or causes it to behave in unintended ways. Might even be a spelling mistake on a label.

Exceptions are different to bugs. Each kind of exception (access violation, stack overflow, and so on) can be raised to a debugger as either a "first chance" or "second chance" exception. First chance exceptions are, by definition, non-fatal unless they are not handled properly with an error handler, at which point they are raised again as a second chance exception (which only a debugger can handle).

If no debugger handles a second chance exception, the application is shut down.

2

You might legitimately raise an exception yourself, you would hopefully never introduce a bug on purpose.

2
  • this reads more like a comment, see How to Answer
    – gnat
    Dec 10, 2014 at 10:03
  • 1
    Brevity doesn't mean that it's not an answer that highlights the difference between the two things.
    – Alan B
    Dec 10, 2014 at 10:21
1

All exceptions are not bugs. It can be a topic of debate that all bugs are exceptions or not.

We can say exceptions are the events that are not part of the normal or expected flow of application. These events can be independent of how the code is written where as a bug is essentially result of bad code(like wrong calculation).

Here is an example of how not handling an exception can be a bug.

Let us suppose there is a program that writes some data to an external storage device. While writing the external storage device was unplugged, crashed or may be destroyed(for what ever reason). Now this is an exceptional case, now regardless the programming language supports exceptional handling or not if the program crashes or misbehaves due to this event, it is a bug.(The end user may have not idea what happended. It is also very unpleasent). But if program aborts the process gracefully, notify the user(in other words handle the exeption) this is clearly not a bug.

The try catch machanism programing languages provide is essentially a tool to ease our way out to handle unexpected events.

1

Synopsis: Exceptions are evidence of bad outcomes, bugs are (some of the) causes of bad outcomes. The problem (to be solved) isn't the exception, the problem is what caused the exception.

Resoning: A bug is a defect in the design or implementation of a product (not limited to software). For example, not using a properly rated relay (time/sensitivity/reliability/capacity) either due to incorrect specs or simple build error. An exception is a real world / run time deviation from predicted (dare I say 'expected'?) behavior, e.g., the loss of control of a vehicle while driving.

Clearly, a bug can cause an exception as the example in 1) could lead to the example in 2). But not all exceptions would be caused by bugs, e.g., loss of control of a vehicle because the operator had a stroke.

0

Since this question has been re-opened for a bounty, let me mention my CUJ article from 2003 entitled "An Exception or a Bug?", which seems to address exactly the OP's question.

Basically, the article defines the terms "bug" and "exception" (giving examples), and proposes strategies for dealing with each.

The article proposes not to "handle" bugs but instead flag them with assertions. In contrast, true exceptions require handling through code (possibly throwing/catching exceptions).

The main point is that bugs require the exact opposite strategy than exceptions.

The aforementioned article is available now from Dr.Dobb's at: http://www.drdobbs.com/an-exception-or-a-bug/184401686

Not the answer you're looking for? Browse other questions tagged or ask your own question.