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