SOEN6441.10.exceptions.pdf - SOEN 6441 Advanced Programming...

This preview shows page 1 - 6 out of 30 pages.

Concordia University Department of Computer Science and Software Engineering ADVANCED PROGRAMING PRACTICES Exception handling Joey Paquet, 2006-2021 1 SOEN 6441 - Advanced Programming Practices
Concordia University Department of Computer Science and Software Engineering Programs are meant to work correctly within their specifications. However, a program might be faced with unforeseen circumstances that are outside of its specifications. Unforeseen situations may come: Externally from the environment of the program When a user or software client tries to use the software outside of its specified usage characteristics. When the program tries to use another piece of software and is faced with unforeseen behavior . Internally from its own execution When the program misbehaves due to an internal logical error and/or being in an inconsistent state. A robust program should be able to handle all kinds of circumstances, foreseen or unforeseen, whether they are coming from the exterior or are a result of its own faults. Exception handling: introduction Joey Paquet, 2006-2021 2 SOEN 6441 - Advanced Programming Practices
Concordia University Department of Computer Science and Software Engineering Exception handling is a mechanism that allows two separately developed program components to communicate when a program anomaly is encountered during the execution of the program. Such communication upon erroneous behavior has been long part of programming practice in the form of error codes and error handling. In error handling, functions set or return special error codes in case of malfunction and finish execution normally. It is then assumed that any function that might be affected will use the error code and react by handling the error i.e. to continue normal execution despite the error. Exception handling: error handling Joey Paquet, 2006-2021 3 SOEN 6441 - Advanced Programming Practices
Concordia University Department of Computer Science and Software Engineering Exception handling: error handling Joey Paquet, 2006-2021 4 SOEN 6441 - Advanced Programming Practices Error handling code can create complexity in simple programs. There can be many different error codes, some error states can even be combinations of more than one error. The error code is a value to be returned. What if the functions also needs to return a value? errorCodeType readFile (){ errorCode = 0 ; open the file; if (theFileIsOpen) { determine the length of the file; if (gotTheFileLength) { allocate that much memory; if (gotEnoughMemory) { read the file into memory; if (readFailed) { errorCode = -1 ; } } else { errorCode = -2 ; } } else { errorCode = -3 ; } close the file; if (theFileDidntClose && errorCode == 0) { errorCode = -4 ; } else { errorCode = errorCode and -4 ; } } else { errorCode = -5 ; } return errorCode ; }
Concordia University Department of Computer Science and Software Engineering However, error handling introduces confusion as it does not enable to separate normal behavior from error-handling behavior.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture