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.

Possible Duplicate:
Who designed exceptions?

Today if we look at any language, exception handling is almost a compulsory feature. The languages that didn't have it earlier are implementing it in their later versions.

I wonder how this idea emerged, because exceptions have a totally separate flow running in parallel with the main code flow and can interrupt the main flow at any point.

IMHO the idea is a bit mind-bending. So, how did this idea surface? A bit of history?

Note to avoid closing(Not worked): And by History I did not mean the dates, times, names and places. I actually wanted to know the informal history that how the idea bubbled up into the languages. Did it came from any sort of programming best practices? Or it was someones idea at the first place?

share|improve this question

marked as duplicate by Péter Török, Walter, Karl Bielefeldt, ChrisF Jul 26 '11 at 10:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
Keep in mind that exceptions are for exceptional conditions, not routine conditions, so the notion that you can "catch" them, regardless of where they occur in your code, can be very useful. Exceptions are not meant to control program flow (to do so is considered a bad practice), but only to provide a flexible pathway for handling problems that are not convenient to handle during normal program execution. –  Robert Harvey Jul 25 '11 at 17:04
    
@Péter Török & others: I am clarifying as in no time this question is going to be closed as exact duplicate of some question which is not exactly same. (Already I can see a close vote.) That question was termed with who, where and when. And the only answer was given accordingly. I want to know how. How did the idea came in the mind of the language designers? Was it a result of some common programming practice? Or anything else? I mentioned history just for aiding the how part. I've not asked for history actually. (Already seeing answers with historical date-time-names!!) –  Gulshan Jul 25 '11 at 17:31
1  
To know how they got the idea in the first place, you'd have to ask them directly, or hope that someone interviewed the person that designed that feature and asked "how did you come up with exception handling" and recorded it for future generations. –  FrustratedWithFormsDesigner Jul 25 '11 at 17:34
    
@FrustratedWithFormsDesigner I think I asked how precisely. May be using the word history was confusing. But it's clear from my previous comment now. Yet anyone is welcome to phrase the question better. –  Gulshan Jul 25 '11 at 17:42
1  
@Glushan: Please Edit your question to be perfectly clear on what you want to know. Long comments are hard to read. Please Edit your question. –  S.Lott Jul 25 '11 at 19:35

3 Answers 3

The first language of which I'm aware that included exception handling was PL/I, in 19641.

It did give exception handling a bit of a bad name, as it used them to handle situations I doubt anybody would treat as exceptions in most modern languages (e.g., reaching the end of a file).

It was prompted largely by Fortran, which (in early versions) provided no way to detect such conditions (including detecting when you'd reached the end of a file).


1It was originally defined in 1964. Actual compilers came somewhat later, and the definition was still in flux until around 1967 or 1968.

share|improve this answer
    
Good one. I forgot about PL/1, although I don't remember if it could throw out of local context to somewhere higher up the call stack. –  Mike Dunlavey Jul 25 '11 at 17:44
    
@Mike: Doing a quick check in my old PL/I book (The PL/I Machine: An Introduction to Programming, Addison Wesley, 1971), yes they could: "All dynamically activated procedures or begin blocks may utilize an established on unit or standard system action as long as no new on statement for the same condition is encountered." Ah, the wonders of being ancient (and having an equally ancient book collection...) –  Jerry Coffin Jul 25 '11 at 18:12
    
Boy am I glad PL/1 is dead. I would rather work in Fortran (and I hate Fortran). –  Mike Dunlavey Jul 25 '11 at 19:01
    
@Mike: I can't blame you there. –  Jerry Coffin Jul 25 '11 at 19:13

THROW was in Lisp in the 70s, and longjmp was in C, paired with setjmp around the same time.

Added: You're asking how they got the idea.

OK, if you're coding away in any language, writing routines, calling routines, trying to figure out how to handle conditions you didn't expect, you have options like "set a global error variable and return", or "have an error return code that the caller has to check".

Inevitably you start strongly wishing "There's got to be a better way to do this. Couldn't I just blast out of here?" If you're doing any assembly language at all, you know in principle it's very simple.

That's how.

share|improve this answer
3  
I wouldn't necessarily call longjmp and setjmp exception-handling features. –  Maxpm Jul 25 '11 at 17:28
    
@Maxpm: Weren't they predecessors? The idea is, if you get in trouble, you can jump back, throwing away stack, to a known good state. –  Mike Dunlavey Jul 25 '11 at 17:40

Well, it emerged as a debugging tool but soon made it's way into run-time implementation in languages like LISP and C.

There are however, multiple types of/ways to preform exception handling and it's all based on perspective.

You probably mean exception handling in the form of try-catch statements, however this is also a type of manual exception handling (C++), and it's been around forever:

    int* pA;
    pA = new int[20];
    if( pA == 0 )
       cout<<"Memory could not be allocated!"<<endl;
share|improve this answer
    
That's quite a bad C++ example. Hungarian notation and incorrect to boot (pA is an array), failure to initialize pA, lack of RAII, lack of std:: on cout and endl, but most importantly here: the if statement is always false, and the cout << unreachable. new never returns 0. It may throw std::bad_alloc, though. –  MSalters Jul 26 '11 at 9:51
    
It's not mean't to be complete, ISO C++. I was simply trying to get the point across. You're over-analyzing it. Furthermore, notation is a personal preference. I use Hungarian within personal projects, but when I code for other projects I use whatever notation they have for the sake of consistency. I use the "if( pA == 0 )" out of habit. As a rule of thumb, you should always check for null pointers. –  David Young Jul 27 '11 at 16:08

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