<p>CmSc 315 Programming languages</p><p>Chapter 7 . Semantics </p><p>7.6 Input/Output semantics</p><p>IO – hardware dependent IO routines included in standard libraries</p><p>Sequential files – Sequence of characters Formatted files Random access files – fixed length records</p><p>Standard IO files: Input stream Output stream Error file</p><p>Random access files Non-sequential processing Disk storage devices: Tracks, Sectors Cylinders</p><p>IO error handling</p><p>Exception handling mechanisms Types of errors: See table 7.4 on p. 179</p><p>User defined exceptions</p><p>7.7. Exception Handling Semantics</p><p>1. Definitions</p><p>Purpose • Simplify programming • Make applications more robust.</p><p>Robust: Able to continue to operate under all conceivable error situations</p><p>1 Exception: An error condition occurring from an operation that cannot be resolved by the operation itself.</p><p>To throw an exception: to signal that the condition represented by the exception has occurred</p><p>To catch an exception: to transfer control to an execution handler that responds to the exception</p><p>2. Strategies and Design Issues</p><p>Early languages – no software tools, error conditions are processed through hardware interrupts</p><p>Modern languages – have constructs to handle exceptions</p><p>Issues: 1. How to define exception handlers 2. How is each handler associated with the exception it has to process 3. Resume or terminate after an exception has occurred</p><p>How to define exception handlers User code to check for exceptions (e.g. if- statement in front of the operation that can produce an exception condition Libraries defined in the language – to signal the exception – to process the exception - use predefined methods (defined in the libraries) - write code to handle the exception</p><p>How is each handler associated with the exception it has to process Search for the handler up the calling sequence</p><p>Resume or terminate after an exception has occurred</p><p>Java: terminates the method that has caused the exception</p><p>Try – catch construct – if present, it specifies what to do and the program may continue </p><p>2 Example: public class EmptyStackException extends RuntimeException { //.------public EmptyStackException() { super ("The stack is empty."); }</p><p>//------// Creates the exception with the specified message. //------public EmptyStackException (String message) { super (message); } }</p><p>/*------pop()------*/ public T pop() throws EmptyStackException { if (isEmpty()) throw new EmptyStackException();</p><p>T result = top.getElement(); top = top.getNext(); count--;</p><p> return result; }</p><p>Upper level - calling program: a) if(!testStack.isEmpty()) { number = testStack.pop(); System.out.println("popped number = " + number); } else System.out.println("The stack is empty"); break;</p><p> b) number = testStack.pop(); System.out.println("popped number = " + number); c) try { ….. number = testStack.pop(); System.out.println("popped number = " + number); …..</p><p>3 }catch(EmptyStackException e) { System.out.println("The stack is empty"); // or System.err.println("The stack is empty");</p><p>} </p><p>Assertions</p><p>4 Using Asserts</p><p>5</p>
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages5 Page
-
File Size-