Exception Contracts Material Transformation

Exception Contracts Material Transformation
Slide Note
Embed
Share

Rationale for exceptions, transforming preconditions to defined behavior, handling undefined behavior, reflection on exceptions and contracts, with code examples illustrating exceptional postconditions and masking techniques.

  • Exception handling
  • Precondition transformation
  • Defined behavior
  • Exception reflection
  • Contracts

Uploaded on Feb 19, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Exception Contracts Material from Liskov Chapter 4 Paul Ammann

  2. Rationale for Exceptions Preconditions document undefined behavior Undefined behavior Partial specification In any implementation, something happens Should clients rely on undocumented behavior? What happens in next release? Bottom line: Preconditions are usually undesirable But sometimes unavoidable Exception handling: Transform preconditions to defined behavior One of several possible mechanisms 2

  3. Transforming a Precondition to an Exceptional Postcondition Key: Postcondition defines behavior for inputs excluded by precondition public double sqrt (double x) // precondition: x >= 0 // postcondition: public double sqrt (double x) // precondition: // postcondition: If x < 0 throw IllegalArgumentException // else public double sqrt (double x) /** * @param x the square of the intended result * @return approximate square root of x * @throws IllegalArgumentException if x < 0 */ 3

  4. Exceptions and Contracts An exception visible to client must appear in the contract We say that the exception is reflected An exception not visible to client must not appear in the contract We say that the exception is masked One exception does not follow this rule: AssertionError Used to indicate that the computation is broken 4

  5. Reflection Example public static int min (int[] a) throws NPE,EE { // Effects if a is null throw NPE else if a is empty throw EE else return min value in a int m; try { m = a[0];} catch (IOOBE e) { throw new EE ( Arrays.min ); } for (int i=0; i < a.length; i++) if (a[i] < m) m = a[i]; return m;} 5

  6. Masking Example public static boolean sorted (int[] a) throws NPE { // Effects if a is null throw NPE else if a is sorted in ascending order return true else return false int p; try { p = a[0];} catch (IOOBE e) { return true; } for (int i=0; i < a.length; i++) if (p < a[i]) {p = a[i];} else {return false;} return true;} 6

More Related Content