Exceptions

Exceptions - COP4020 Programming Languages Exception...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
COP4020 Programming Languages Exception Handling Prof. Robert van Engelen
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COP4020 Spring 2011 2 4/13/11 Overview ! What is “defensive programming”? ! Ways to catch and handle run-time errors: " In programming languages that do not support exception handling " Exception handling in C++ " Exception handling in Java
Background image of page 2
COP4020 Spring 2011 3 4/13/11 Defensive Programming ! Defensive programming is a methodology that makes programs more robust to failures " Meant to increase the reliability and quality of software " Writing programs while in a state of “constant alert for trouble”, meaning add code to check for all possible runtime failures ! Runtime failures may be due to " Erroneous user input (e.g. entering a date in the wrong format) " File format and access problems (e.g. end of file or disk full) " Networks failures " Problems with arithmetic (e.g. overflow) " Hardware and software interrupts (e.g. hitting the break key)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COP4020 Spring 2011 4 4/13/11 Exception Handling Principles ! The three purposes of an exception handler: 1. Recover from an exception to safely continue execution 2. If full recovery is not possible, display or log error message(s) 3. If the exception cannot be handled locally, clean up local resources and re-raise the exception to propagate it to another handler ! Exception handling makes defensive programming easier " An exception is an error condition, failure, or other special event " Exceptions are handled by exception handlers to recover from failures " Built-in exceptions are automatically detected by the run-time system and handled by internal handlers or the program aborts " Exceptions can be explicitly raised by the program " Exception handlers can be user-defined routines that are executed when an exception is raised
Background image of page 4
COP4020 Spring 2011 5 4/13/11 Signal Handlers ! UNIX signal handlers are a very simple form of exception handlers " The signal () function allows signals to be caught or ignored: signal(SIGINT, SIG_DFL); // default setting signal(SIGINT, SIG_IGN); // ignore signal(SIGINT, handle_quit); // use handler void handle_quit(int sig) { … } ! Some useful signals: " SIGINT interrupt program " SIGFPE floating point exception " SIGKILL kill program " SIGBUS bus error " SIGSEGV segmentation violation " SIGPIPE write on pipe with no reader " SIGALRM real-time timer expired
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COP4020 Spring 2011 6 4/13/11 Catching Runtime Errors w/o Exception Handlers ! C, Fortran 77, and Pascal do not support exception handling ! Other mechanisms to handle errors add "clutter” and obscures the logic of a program !
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/01/2012 for the course COP 4020 taught by Professor Engelen during the Spring '11 term at FSU.

Page1 / 18

Exceptions - COP4020 Programming Languages Exception...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online