Langage de Programmation 2   (LP2  ) - RICM3   Cours 5   Flots et  analyse syntaxique, Exceptions
49 pages
Français

Langage de Programmation 2 (LP2 ) - RICM3 Cours 5 Flots et analyse syntaxique, Exceptions

-

Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres
49 pages
Français
Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres

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 ...

Informations

Publié par
Nombre de lectures 48
Langue Français

Extrait

’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>
raise exceptionListeVide
lethdl=matchlwith |Cons(x,l)x |NilraiseListeVide
#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.
Important
Une exception peut transporter de l’information :
exceptionAlarmeofint*bool
Que faire de ces excetpions ....
8 / 55
LnaEgxacgeeptdieonPsrogrmamation2(L2P)
Récupération d’exception (évaluation)
tryEwith |ListeVideE1 |Alarme(n,b)E2(nb) |Not_foundE3 |Division_by_zeroE4
=
T
(une expression)
Évaluation Eest évalué en premier, si aucune exception levéevaleur deT=valeur deE exception levéeefiltrée dans la partiewith filtrage réussi auiemotifvaleur 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)
9/55
LanEgxacgeeptdienoPsrogrmaamiton2L(2P)
Deux “lanceurs prédéfinis”
failwith "toto"raise (Failure "toto")
invalid_arg "toto”raise (Invalid_argument
"toto")
10 / 55
naqt,Teu)sSinon)|Tantquecmoipl,iSlAroorncefeDco(ilmpi,b(r)1esiaEsaProSsiSlAq|),eSpilcomDef(core(feDerocnEsaPesira)i21,,i(bonin(fcmoipnEocereDraisePasec(v,e)PesinEsa2i,1ar)|S)(ieq,lecA11/55
On peut alors tester le programmeglobaldès le départ proposer le vrai code pourchaque cas pris séparément
Objectif :problèmes plus petits traités un à unse ramener à des exceptionPasEncoreDefofstring*string
Utilisation d’exceptions en mise au point
letDefe(ocaPEscnroide)|Ampil,Vm=ilhctacceripmordeseaiitiwVih|rogredePtionamma)2xE(2PLoisnectpgagnaL
LanEgxacgeeptdieonPsrgormaamitno2(
Application
L2P)
Écrire une fonction prenant une liste d’entiers et renvoyant la somme des ses éléments ; si la liste contient un négatif alors renvoyer1.
Un premier programme
let rec somme l = match l with [] -> 0 |x::r -> if x < 0 then -1 else x + (somme r);;
ne fonctionne pas
12 / 55
  • Univers Univers
  • Ebooks Ebooks
  • Livres audio Livres audio
  • Presse Presse
  • Podcasts Podcasts
  • BD BD
  • Documents Documents