In some programming languages, such as Java, an exception is a type of message that signifies that Something Potentially Bad Just Happened.

Exceptions are "thrown", and they're "caught". In Java, the usual programming idiom for writing code that can potentially do something bad is try { ... } catch (Exception e) { ... }, as seen here:

   /* This is for some embedded system that controls a
      ping-pong ball machine. Should be put inside an
      infinite loop or something.
   */

   try {
      waitPatientlyForSomeSeconds();
      spewOutPingPongBallsInConstantStream(100000);
   } catch (OutOfPingPongBallsException ppbe) {
       // Handle a non-fatal exception.
       robot.fillBallContainer();
   } catch (BallThrowerStuck stucke) {
       // Handle fatal exception.
       sendAlert("That ball machine is down again. " +
                 "Please send someone to fix it...");
       haltAndWaitForReboot();
   } catch (Exception e) {
       // Catch all other exceptions not covered here
       sendAlert("Other exception: " + e + " Need help?");
       haltAndWaitForReboot();
   } finally {
       // Well, something could be done after all this.
       // I couldn't come up with anything sensible.
       // (This used to set done=true or something silly
       // like that. =)
   }

When an exception is thrown by a subroutine, it's passed up in the "chain of command" until some catcher takes responsibility of it. If it's not caught, the exception is caught by OS or (in case of JVM) the interpreter.

Not all exceptions are fatal, as seen in the above example; this is why exceptions can be caught and they can be reacted accordingly.

See also: Error.

Exception is the replacement for the goto statement. It also gives you a complementary comefrom statement, something that goto seriously lacked.

Oh, btw:

try {
   ..
} catch (.... e) {
   done = true;
} ... {
  ...
} finally {
   done = false
}

Since finally blocks are run ALL THE TIME, the "done=true" statement will have NO effect whatsoever, as the finally statement sets it back to false.

"Exception" as an English word

An unusual, remarkable occurrence. A low-probability occurrence. e.g.
Yes, people do occasionally survive falling out of an aeroplane without a parachute, but that's the exception rather than the rule.

Something that is not usually done, a bending of the rules, usually in someone's favour. e.g.
You told me again that you preferred handsome men, but for me you would make an exception.

See also exceptional, which unless explicitly stated as exceptionally bad, has the connotation of good, above average. e.g.
The intensive school was a place for exceptional students.  Exceptionally good
It was a wonderful, exceptional day.  Exceptionally good
I have seen many decaying ruins, and this one is exceptionally decayed.   Exceptionally decayed.

"To take exception to" is a formal, rather Victorian English phrase indicating that you feel insulted, that the utterance was beyond what is acceptable, e.g. "Sir, I take exception to your remarks concerning my mother's sexual practices. I challenge you to a duel."

"Exception" as programming term

yes, this is the main aim of my writeup
An exception is a procedural programming language construct designed to deal with unusual or error conditions. Exceptions in the programming sense are usually bad. e.g. The program threw an exception and crashed.

An exception can be seen as a goto in disguise. While this is true, it is worth bearing in mind that all flow of control statements, if then else blocks, for loops, while loops, case etc. are all just gotos in disguise. All of them are improvements over using raw unstructured gotos, which are considered harmful for general use, and are unnecessary in almost all cases if you have structured replacements for them. Exceptions are no different, and are well worth using instead of trying to inspect the return value of every function that you call. Bruce Eckel explains:

If you were thorough enough to check for error every time you called a method, your code would turn into an unreadable nightmare.

The word "exception" is meant in the sense of "I take exception to that." At the point where the problem occurs you might not know what to do with it, but you know that you can't just continue on merrily, you must stop and somebody, somewhere, must figure out what to do.

.. they clean up error handing code. Instead of checking for a particular error and dealing with it in multiple places in your program, you no longer need to check at the method call ... you need to handle the problem at only one place, the exception handler
- from Thinking in Java, chapter 10 "error handling with exceptions" by Bruce Eckel.

An exception is thrown when an error occurs. Normal program flow is interrupted, and control passes to the nearest enclosing exception handler, then normal flow of control resumes. An exception is thrown due to the code explicitly doing so, or due to an interrupt from the operating system. If this sounds odd and hard to grasp, then look at the examples first.

Since the popularity of Exceptions has come about after the popularity of Object-Oriented programming, in most languages the exception is some kind of Object, which can be queried as to its type and properties.

I will do some examples in a programming language that I use, Delphi. Delphi's exception model is quite close to that of Python and C#. Java uses a substantially similar model, with the following minor differences:

  • Java's catch keyword, with a different syntax, is used instead of except to catch and handle exceptions.
  • All java methods must declare the exceptions that they may throw. When calling a method, the caller method must either catch all exceptions that the callee throws, or else must in turn declare them in it's own throws list. This is enforced by the compiler.
Your mileage may vary in other languages. Anyway, here are the Delphi code examples:

// a function that crashes, but the program will recover
function ArithmeticException: Double;
var
  MyZero: Double;
begin
  MyZero := 0;
  Result := 10 / MyZero; // bang! - an exception will be thrown!!!
  ShowMessage('You will not see this');
end;

procedure TryIt;
begin
  try
    ArithmeticException;
  except
    ShowMessage('Bang');
  end;
end;

What happens when TryIt is called? At the division by zero, an exception is thrown. The normal flow of execution stops, the stack is unwound until the nearest enclosing try-except block is found. In this case it's not far away. The result of calling TryIt is simply the message "Bang".

Finally blocks

The other form of exception handling, the try..finally block is used to ensure that a piece of code is run no matter what, even if an exception is thrown. This is usually used to ensure that allocated resources are freed.  For instance, if your code reads:

function ThisMayFail: Double;
var
  lcSomeObj: TBigExpensiveObject;
begin
  lcSomeObj := TBigExpensiveObject.Create;

  lcSomeObj.SomeRiskyOperation;
  lcSomeObj.SomeRiskyOperation;

  Result := lcSomeObj.GetResultValueFromTheNetherDepths;

  lcSomeObj.Free;
end;

If an exception is thrown during some risky operation, the object will never be freed, and memory will leak. The following code is better:

function ThisMayFail: Double;
var
  lcSomeObj: TBigExpensiveObject;
begin
  lcSomeObj := TBigExpensiveObject.Create;
  try

    lcSomeObj.SomeRiskyOperation;
    lcSomeObj.SomeRiskyOperation;

    Result := lcSomeObj.GetResultValueFromTheNetherDepths;
  finally
    lcSomeObj.Free;
  end;
end;

Now you are guaranteed that in normal execution, or even if an exception is thrown, in fact always (barring a catastrophe such as a power failure, OS crash or the user forcibly killing the entire program, in which case the memory leak won't matter) the object will be freed. This will happen either in the normal sequence of statements, or on the way out while throwing an exception.

Advanced try..except:

There are several common variations on the try..except block:


You can manually raise an exception if you think it is warranted.

if pObject = nil then
   Raise TMyException.Create('Nil pointer in someproc');

This also demonstrates that you can make your own subclasses of Exception, and test for them as shown in examples below.


You can squash exceptions.


procedure TryIt;
begin
  try
    ArithmeticException;
  except
  // on error, do nothing
  end;
end;

This style is one of my pet hates. Try-except blocks are not a substitute for good code or debugging. This is not error-free code, this is bad code that lies about its errors. Errors should be reported so that they can be fixed, for e.g.

procedure TryIt;
begin
  try
    ArithmeticException;
  except
   on
E: Exception do
   
ShowMessage('Failed ArithmeticException with error ' + E.ClassName + ': ' + E.Message);
  end;
end
;

Which produces a nice error message "Failed ArithmeticException with error EZeroDivide: Floating point division by zero". If a dialog box is not appropriate for your program (perhaps it is a server task that runs unattended and must keep running) then log the error and carry on. Just don't ignore it.

This also illustrates that Exceptions are objects.


You can be selective about which exception objects you catch, based on their type (all Exceptions are of type Exception, or one of its sublasses). this is also good practice - you don't want to tell the user that the text that they entered cannot be converted to a string when actually the error was that the OS has run out of memory:

procedure TryIt;
begin
  try
    ArithmeticException;
  except
    on
E: EMathError do
    
ShowMessage('Failed with a math error ' + E.ClassName + ': ' + E.Message)
    else
      Raise;
  end;
end
;

In this case, EMathError  is a base class for EZeroDivide and all other things that can go wrong with numbers. All other errors will be passed on to the next enclosing try..except block. If none is found, then the default handler which encloses the entire program will be invoked and show you a nasty dialog box.

Ex*cep"tion (?), n. [L. exceptio: cf. F. exception.]

1.

The act of excepting or excluding; exclusion; restriction by taking out something which would otherwise be included, as in a class, statement, rule.

2.

That which is excepted or taken out from others; a person, thing, or case, specified as distinct, or not included; as, almost every general rule has its exceptions.

Such rare exceptions, shining in the dark, Prove, rather than impeach, the just remark. Cowper.

Often with to.

That proud exception to all nature's laws. Pope.

3. Law

An objection, oral or written, taken, in the course of an action, as to bail or security; or as to the decision of a judge, in the course of a trail, or in his charge to a jury; or as to lapse of time, or scandal, impertinence, or insufficiency in a pleading; also, as in conveyancing, a clause by which the grantor excepts something before granted.

Burrill.

4.

An objection; cavil; dissent; disapprobation; offense; cause of offense; -- usually followed by to or against.

I will never answer what exceptions they can have against our account [relation]. Bentley.

He . . . took exception to the place of their burial. Bacon.

She takes exceptions at your person. Shak.

Bill of exceptions Law, a statement of exceptions to the decision, or instructions of a judge in the trial of a cause, made for the purpose of putting the points decided on record so as to bring them before a superior court or the full bench for review.

 

© Webster 1913.

Log in or registerto write something here or to contact authors.