-
49
pages
-
Français
-
Documents
Description
Langage de Programmation 2 (LP2)Langage de Programmation 2 (LP2)RICM3Cours 5Flots et analyse syntaxique, ExceptionsPascal LafourcadePolytech2009 - 20101 / 55Langage de Programmation 2 (LP2)PlanExceptionsMotivationLangagesAnalyse syntaxiqueFlotsConclusion2 / 55Langage de Programmation 2 (LP2)ExceptionsQue faire?Toute fonction qui ne boucle pas renvoie une valeur.# 1/0;;# List.hd []Utiliser le type optiontype ’a option = None| Some of ’aÉtend un type avec une valeur None.Limite : propagation.4 / 55Langage de Programmation 2 (LP2)ExceptionsQue faire?Toute fonction qui ne boucle pas renvoie une valeur.# 1/0;;Exception: Division_by_zero.# List.hd []Exception : Failure "hd".Solution les Exceptions5 / 55Langage de Programmation 2 (LP2)Exceptionsfailwith# failwith;;- : string -> ’a = Exemple# let tete = function[] -> failwith "tete vide"| x::l -> x ;;val tete : ’a list -> ’a = # tete [];;Exception: Failure "tete vide".6 / 55Langage de Programmation 2 (LP2)Exceptionsfailwith à la loupe# let failwith s = raise (Failure s);;failwith : string -> ’a = raiseexception ListeVidelet hd l = match l with| Cons (x, l)→ x| Nil→ raise ListeVide#exception Erreurfatale of string;;exception Erreurfatale of string#raise (Erreurfatale "crash compilateur");;Exception: Erreurfatale "crash compilateur".7 / 55Attention au MAJUSCULELangage de Programmation 2 (LP2)ExceptionsLe type des exceptionsListeVide : de type ...
-
Publié par
-
Langue
Français