Exceptions General mechanism for handling abnormal conditions Predefined exceptions constraint violations I O errors communication errors other illegalities User defined exceptions for robust abstractions Predefined exception raised by the runtime system User defined exception can be raised thrown by user code Exception handlers specify remedial actions or proper shutdown Exceptions can be stored and re raised later concurrency 1 Predefined exceptions in Ada Defined in Standard Constraint Error value out of range Program Error illegality not detectable at compile time unelaborated package exception during finalization Storage Error allocation cannot be satisfied heap or stack Tasking Error communication failure Defined in Ada IO Exceptions Data Error End Error Name Error Use Error Mode Error Status Error Device Error concurrency 2 Handling exceptions Any begin end block can have an exception handler procedure test is x integer 25 y integer 0 begin x x y exception when Constraint Error Put Line as expected when others Put Line out of the blue end concurrency 3 A common idiom with Integer Text Io use Integer Text Io function Get Data return Integer is X Integer begin loop as long as input is not legal begin Get X return X if got here input is valid exception when others Put Line input must be integer try again end end loop end concurrency 4 Exception propagation When an exception is raised the current sequence of statements is abandoned If an exception handler for the current exception is present it is executed and the current frame is completed Otherwise the frame is discarded and the enclosing dynamic scopes are examined to find a frame that contains a handler for the current exception If none is found the program terminates The current frame is never resumed concurrency 5 User defined Exceptions Client server contract if inputs are proper either the output is correct or else client is notified of failure The inputs are the responsibility of the client the caller package Stacks is Stack Empty exception package body Stacks is procedure Pop X out Integer From in out Stack is begin if Empty From then raise Stack Empty else concurrency 6 The scope of exceptions Exception has the same visibility as other declared entities to handle an exception it must be visible in the handler An others clause can handle unamable exceptions partially when others Put Line disaster somewhere raise propagate exception program will terminate concurrency 7 Exception information An exception is not a type we cannot declare exception variables and assign to them An exception occurrence is a value that can be stored and examined an exception occurrence may include additional information source location of occurrence contents of stack etc Predefined package Ada Exceptions contains needed machinery concurrency 8 Ada Exceptions package Ada Exceptions is type Exception Id is private type Exception Occurrence is limited private function Exception Identity X Exception Occurrence return Exception Id function Exception Name X Exception Occurrence return String procedure Save Occurrence Target out Exception Occurrence Source Exception Occurrence procedure Raise Exception E Exception Id Message in String concurrency 9 Using exception information exception when Expected Constraint Error Save Occurrence Event Log Expected when Trouble others Put Line unexpected Exception Name Trouble raised Put Line shutting down raise concurrency 10 Exceptions in C Same runtime model Exceptions are classes Handlers appear in try blocks try some complex calculation catch range error range error might be raised in some complex calculation cerr oops n catch zero divide ditto for zero divide cerr why is x zero n concurrency 11 Defining and throwing exceptions The program throws an object There is nothing in the declaration to indicate it will be used as an exception struct Zero Divide public int lineno information Zero Divide useful constructor try if x 0 throw Zero Divide constructor and go call concurrency 12 Exceptions and inheritance A handler names a class and can handle an object of a derived class as well class Matherr a bare object no info class Overflow public Matherr class Underflow public Matherr class Zero Divide public Matherr try weather prediction model who knows what will happen catch Overflow e g change parameters in caller catch Matherr Underflow Zero Divide handled here catch handle anything else ellipsis concurrency 13 Exceptions in Java Model and terminology similar to C exception are objects that are thrown and caught try blocks have handlers which are examined in succession a handler for an exception can handle any object of a derived class Differences all exceptions are extension of predefined class Throwable checked exceptions are part of method declaration the finally clause specifies clean up actions concurrency 14 If a method might throw an exception callers should know about it public void replace String name Object newvalue throws NoSuch mandatory declaration Attribute attr find name if attr null throw new NoSuch name throw build before newvalue update attr Caller must have a handler for NoSuch or else must be declared as throwing NoSuch itself Compiler checks Only required for checked exceptions not predefined ones which are extensions of RuntimeException and Error concurrency 15 Mandatory cleanup actions Some cleanups must be performed whether the method terminates normally or throws an exception public void encrypt String file throws StreamException Stream input try input new Stream file iterator Words new iterator input for word w Words init Words more w Words next RSAencode w may fail somewhere finally if input null input close how we exit regardless of concurrency 16 Concurrent programming tasks and threads Concurrent Programming Declaration creation activation termination Synchronization and communication Time and delays conditional communication Non determinism concurrency 17 Concurrent programming Synchronous and asynchronous models of communication Description of simultaneous independent activities A task is an independent thread of control with own stack program counter and local environment Ada Tasks communicate through Rendez vous protected objects Shared variables Java threads communicate through shared objects preferably synchronized concurrency 18 Task Declarations A task type is a limited type task type worker type Worker Id is access worker task body worker is begin loop compute end loop
View Full Document
Unlocking...