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.