Microsoft PowerPoint - Tutorial 8 ML Exceptions.ppt
Short Description
Tutorial Notes: Standard ML. Notes. ML Exceptions. 2. ML Exceptions.3. Exceptions. Exceptions. Instead of using the return value to report an error we will …
Website: webcourse.cs.technion.ac.il | Filesize: 26kb
Content
234319: Programming Languages.
Tutorial Notes: Standard ML
Notes
ML Exceptions
1
ML Exceptions.
1
Standard ML
Exceptions
ML Exceptions.
2
Exceptions - The Need
An extensive part of the code is error handling
A function F can return a problem solution (like int) or fail to
find the solution or find that a solution does not exists.
We can try to use ML datatypes:
datatype sol = Success of int
| Failure | Impossible;
Using the failure return values can be tedious
case methodA(problem) of
Success s => show s
| Failure => (case methodB(problem) of
Success s => show s
| Failure => “Both failed”
| Impossible => “No Good”)
| Impossible => “No Good”234319: Programming Languages.
Tutorial Notes: Standard ML
Notes
ML Exceptions
2
ML Exceptions.
3
Exceptions
Instead of using the return value to report an error we will
use a mechanism outside the return value: Exceptions.
When an error is discovered we will raise an exception
The exception will propagate up the stack until someone
handles it.
The caller of a function doesn’t have to check all or any of
the error values.
In VERY pseudo-code:
fun inner = do calculation
if local error raise local_error,
if global error raise global_error
fun middle = inner(.) handle local_error
fun outer = middle(.) handle global_error
ML Exceptions.
4
Exceptions in ML
We can raise only a specific type: the built-in type exn.
exn is a datatype with an extendable set of constructors.
Declaring exceptions
exception Failure;
exception Impossible;
exception Problem of int;
Defines the following constructors:
Failure : exn;
Impossible : exn;
Problem : int -> exn;
Can be declared locally using let
Values of type exn have all the privileges of values of
other types, and in addition, a special role in the
operations raise…
Get the file Download here
Related Books:Related Searches: standard ml, global error, error values, pseudo code, problem solution
Comments
Leave a Reply