lec7 - COMP201 Java Programming Topic 7: Exceptions...

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

View Full Document Right Arrow Icon
COMP201 Java Programming Topic 7: Exceptions Reading: Chapter 11
Background image of page 1

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

View Full DocumentRight Arrow Icon
COMP201 Topic 7 / Slide 2 Outline Introduction Java exception classes Dealing with exceptions Throwing exceptions Catching exceptions
Background image of page 2
COMP201 Topic 7 / Slide 3 Introduction Causes of errors User input errors: typos, malformed URL, wrong file name, wrong info in file… Hardware errors: Disk full, printer out of paper or down, web page unavailable… Code errors: invalid array index, bad cast, read past end of file, pop empty stack, null object reference…
Background image of page 3

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

View Full DocumentRight Arrow Icon
COMP201 Topic 7 / Slide 4 Introduction Goals of error handling Don’t want: Want: Return to a safe state and enable user to execute other commands Allow user to save work and terminate program gracefully.
Background image of page 4
COMP201 Topic 7 / Slide 5 Introduction Java exception handling mechanism: Every method is allowed to have two exit paths No errors occur Method exits in the normal way Returns a value – Control passed to the calling code. If errors occur – Method exits via an alternative exit path – Throws an object that encapsulates the error information Control passed to exception mechanism that searches for an appropriate exception handler to deal with the error condition
Background image of page 5

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

View Full DocumentRight Arrow Icon
COMP201 Topic 7 / Slide 6 Exception object is created when an error occurs Exception object contains information about the exception The object is handed off to the runtime system Runtime system searches for the method that contains an appropriate exception handler
Background image of page 6
COMP201 Topic 7 / Slide 7 Advantage Separate error handling code from regular code Sample code without error handling traditional error detection, reporting, and handling often lead to messy code readFile { open the file; determine its size; allocate that much memory; read the file into memory; close the file; }
Background image of page 7

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

View Full DocumentRight Arrow Icon
Traditional Approach errorCodeType readFile { initialize 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; } Java Exception Handling Approach readFile { try { open the file; determine its size; allocate that much memory; read the file into memory; close the file; } catch (fileOpenFailed) { doSomething; } catch (sizeDeterminationFailed) { doSomething; } catch (memoryAllocationFailed) { doSomething; } catch (readFailed) { doSomething; } catch (fileCloseFailed) { doSomething; } } It does NOT spare your effort to detect, report and handle the exception, but provide an elegant framework.
Background image of page 8
COMP201 Topic 7 / Slide 9 Java Exception Class JVM internal errors
Background image of page 9

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

View Full DocumentRight Arrow Icon
COMP201 Topic 7 / Slide 10 Java Exception Classes
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 31

lec7 - COMP201 Java Programming Topic 7: Exceptions...

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

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