CS 476 – Programming Language Design William Mansky
Errors and Exceptions • Runtime errors are represented by getting stuck and no other rules for variables • Type safety: well-typed programs don’t get stuck • But in the real world, well-typed programs can still have errors! 1
Errors and Exceptions E : : = <#> | <ident> |E+E|E–E|E*E |… |E/E 2
Semantics of Division • How can we still have type safety? 3
Stronger Type System • 4
Different Semantics • Change the semantics so division by 0 doesn’t get stuck • This is okay, but might be surprising to programmers! 5
Different Semantics • Change the semantics so division by 0 doesn’t get stuck • An exception is an error that doesn’t have to break the whole program 6
Exception Semantics • Values: ints, bools, exc 7
Exception Semantics: Commands C : : = <ident> : = E | C; C | skip | if E then C else C | while E do C | throw 8
Exception Handling C : : = <ident> : = E | C; C | skip | if E then C else C | while E do C | throw | try C catch C 9
Exception Handling: Semantics 10
Exception Handling: Final States • Well-typed programs now don’t get stuck • A program can evaluate to a state, or a state + an uncaught exception • type result = Success of state | Exc of state • eval_cmd : cmd -> state -> result option • We could also have different kinds of exceptions 11