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 ...
’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 ..." />
LangagedeProgrammation2L(2P)
Langage de Programmation 2 (LP2) RICM3 Cours 5 Flots et analyse syntaxique, Exceptions
Pascal Lafourcade
Polytech
2009 - 2010
1 / 55
LangagedeProg
Plan
rammati
Exceptions
o
Motivation
Langages
n2L(2P)
Analyse syntaxique
Flots
Conclusion
2
/
55
LanEgxacgeeptdieonPsrgormamatino2
Que faire ?
L(P2)
Toute fonction qui ne boucle pas renvoie une valeur.
# 1/0;;
# List.hd []
Utiliser le type option
type ’a option = None | Some of ’a
Étend un type avec une valeur None.
Limite : propagation.
4 / 55
LanEgxacgeeptdieonPsrogrmaamitno2
Que faire ?
L(2P)
Toute fonction qui ne boucle pas renvoie une valeur.
# 1/0;;
Exception: Division_by_zero.
# List.hd [] Exception : Failure "hd . "
Solution les Exceptions
5 / 55
LanEgxacgeeptdieonPsrgoramm
failwith
taion2(LP2)
# failwith;; - : string -> ’a = <fun>
Exemple
# let tete = function [] -> failwith "tete | x::l -> x ;;
val tete : ’a list -> ’a =
vide"
<fun>
# tete [];; Exception: Failure "tete vide".
6 / 55
LnaEgxacgeeptdienoPsrgormaamtino2(LP2)
failwith à la loupe
# let failwith s = raise (Failure s);; failwith : string -> ’a = <fun>
#exception Erreurfatale of string;; exception Erreurfatale of string #raise (Erreurfatale "crash compilateur");; Exception: Erreurfatale "crash compilateur".
Attention au MAJUSCULE
7/55
LanEgxacgeeptdieonPsrogrammatino2L(2P)
Le type des exceptions
ListeVide: de typeexn raiseListeVide: de type déterminé en fonction du contexte exnest : ◮un type somme particulier. ◮étendu dynamiquement : exceptionListeVideintroduit un nouveau constructeur deexn.
Évaluation Eest évalué en premier, si ◮aucune exception levée⇒valeur deT=valeur deE ◮exception levéeefiltrée dans la partiewith ◮filtrage réussi auiemotif⇒valeur deT=résultat deEi (en général : une valeur, mais ce peut être une levée d’exception) ◮sinon l’exceptioneest transmise (comme si pas detry)