Unformatted text preview:

ExceptionsPredefined exceptions in AdaHandling exceptionsA common idiomException propagationUser-defined ExceptionsThe scope of exceptionsException informationAda.ExceptionsUsing exception informationExceptions in C++Defining and throwing exceptionsExceptions and inheritanceExceptions in JavaIf a method might throw an exception, callers should know about itMandatory cleanup actionsConcurrent programming: tasks and threadsConcurrent programmingTask DeclarationsSlide 20Task ActivationTask ServicesSynchronization: Rendez-vousExample : semaphoreUsing a semaphoreDelays and TimeTimeConditional CommunicationConditional Communication (ii)Conditional communication (iii)Concurrency in JavaThreads at workActivation and executionExceptionsExceptions 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 - 1Predefined exceptions in AdaPredefined 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 - 2Handling exceptionsHandling 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 - 3A common idiomA 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 - 4Exception propagationException 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 - 5User-defined ExceptionsUser-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 - 6The scope of exceptionsThe 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 - 7Exception informationException 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 - 8Ada.ExceptionsAda.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 - 9Using exception informationUsing exception informationexception 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 - 10Exceptions in C++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 - 11Defining and throwing Defining and throwing exceptionsexceptions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; // useful information Zero_Divide () {…} // constructor … try { … if (x == 0) throw Zero_Divide (..); // call constructor and go concurrency - 12Exceptions and inheritanceExceptions 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 - 13Exceptions in JavaExceptions in JavaModel and terminology similar to C++:◦exception are objects that are thrown and caught◦try blocks have


View Full Document

NYU CSCI-GA 2110 - Lecture Slides

Download Lecture Slides
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Slides and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Slides 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?