Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
    
I hope the technical details I mention help clarify the distinction of an exception vs a bug. Great questionn BTW, +1 –  Jeremy Thompson Nov 5 '11 at 2:07
    
semantics –  Jarrod Roberson Nov 8 '11 at 22:47

6 Answers 6

up vote 60 down vote accepted

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.

share|improve this answer
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? –  Niklas rtz Nov 5 '11 at 3:20
    
@Nicke: Yep, I'd agree with that. –  Dean Harding Nov 5 '11 at 6:00

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.

share|improve this answer
    
Heh, we answered at almost the same time, and with basically the same example as well :-) –  Dean Harding Nov 5 '11 at 0:57
2  
@DeanHarding Great minds think alike, yes? :D –  Glenn Nelson Nov 5 '11 at 1:01
    
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? –  Scott Whitlock Nov 5 '11 at 1:18
    
@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 –  Glenn Nelson Nov 5 '11 at 1:32

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.

share|improve this answer
    
+1 for defining your terms. In general, people should do that more often! –  yfeldblum Jan 7 '12 at 21:56

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.

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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