13Handling exceptions

13Handling exceptions - ITI 1121. Introduction to Computing...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ITI 1121. Introduction to Computing II * Marcel Turcotte School of Information Technology and Engineering Version of February 14, 2011 Abstract Handling errors Declaring, creating and handling exceptions Checked and unchecked exceptions Creating new exception types * These lecture notes are meant to be looked at on a computer screen. Do not print them unless it is necessary. Error processing Errors can occur at compile-time or runtime. Syntax errors are detected at compile time. Since Java is a strongly typed language, the compiler will also ensure that each expression is valid, i.e. only valid operations for this data type are performed, avoiding errors that would otherwise occur during the execution of the program. Although type checking is an efficient way of detecting errors as soon as possible, there are certain verifications that can only be made at execution time ; e.g. checking that a stack contains an element before removing it. In Java, runtime errors are handled by a mechanism called Exceptions. Sources of errors Logic of the program; External events: running out of memory, write error, etc. Detecting and handling errors will make our programs more robust. Ideally, those mechanisms should help locating precisely the source of the errors. For instance, the method pop of the ArrayStack implementation, could throw an IndexOutOfBoundsException . Is the source of the error necessarily in the method pop ? No. In fact, it is likely that the error situation occurred because the caller did not check if the stack was empty before calling the method pop . Pre-conditions Pre-conditions are the ensemble of conditions that the parameters and the state of the object, must verify so that the computation can be successful. A familiar example is the calculation of the square root of a number. The pre-condition is that the parameter be a non-negative number. Depending on the programming language used or the library, the validity of the calculation is not guaranteed: some programs may loop, others may crash. In the case of ArrayStack , an attempt to remove an element when the stack is empty is an example of an error condition; here, the state of the object was inconsistent with the actual goal of the method pop . Its a good programming habit to always think about the preconditions of a method. From now on, try to identify the preconditions when writing a method. Of the value of top. . . Handling errors What should we do? Crash the program (e.g. 1 / ), exit, . . . certainly not. Write an error message? Good idea but it is not enough. Traces are useful during the development of a program but should be removed from its final version. Handling errors It would be possible to return a special value, sentinel , whenever an error is detected....
View Full Document

Page1 / 57

13Handling exceptions - ITI 1121. Introduction to Computing...

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

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