c# : exceptions...plan 1 introduction 2 capture d’exception 3 les exceptions personnalisees´ 4...

42
C# : exceptions Achref El Mouelhi Docteur de l’universit ´ e d’Aix-Marseille Chercheur en programmation par contrainte (IA) Ing ´ enieur en g ´ enie logiciel [email protected] H & H: Research and Training 1 / 30

Upload: others

Post on 02-Apr-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

C# : exceptions

Achref El Mouelhi

Docteur de l’universite d’Aix-MarseilleChercheur en programmation par contrainte (IA)

Ingenieur en genie logiciel

[email protected]

H & H: Research and Training 1 / 30

Page 2: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

Plan

1 Introduction

2 Capture d’exception

3 Les exceptions personnalisees

4 Les instructions multi-catch

5 Les exceptions parametrees

6 Le bloc finally

H & H: Research and Training 2 / 30

Page 3: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Introduction

C#

Exception

C’est une erreur qui se produit pendant l’execution de notreprogramme

Une exception dans un programme implique generalement sonarret d’execution

H & H: Research and Training 3 / 30

Page 4: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Introduction

C#

Comment faire pour poursuivre l’execution?

Reperer les blocs pouvant generer une exception

Capturer l’exception correspondante

Afficher un message relatif a cette exception

Continuer l’execution

H & H: Research and Training 4 / 30

Page 5: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Introduction

C#Exception : exemple

class Program {static void Main(string[] args){int x = 0;int y = 5 / x;Console.WriteLine(x);Console.WriteLine("Fin de calcul");

}}

Le message affiche a l’execution

Exception non geree : System.DivideByZeroException : Tentative de division par zero....Program.cs :ligne 40

Constatation

Le message Fin de calcul n’a pas ete affiche

La division par zero declenche une exception DivideByZeroException

H & H: Research and Training 5 / 30

Page 6: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Introduction

C#Exception : exemple

class Program {static void Main(string[] args){int x = 0;int y = 5 / x;Console.WriteLine(x);Console.WriteLine("Fin de calcul");

}}

Le message affiche a l’execution

Exception non geree : System.DivideByZeroException : Tentative de division par zero....Program.cs :ligne 40

Constatation

Le message Fin de calcul n’a pas ete affiche

La division par zero declenche une exception DivideByZeroException

H & H: Research and Training 5 / 30

Page 7: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Introduction

C#Exception : exemple

class Program {static void Main(string[] args){int x = 0;int y = 5 / x;Console.WriteLine(x);Console.WriteLine("Fin de calcul");

}}

Le message affiche a l’execution

Exception non geree : System.DivideByZeroException : Tentative de division par zero....Program.cs :ligne 40

Constatation

Le message Fin de calcul n’a pas ete affiche

La division par zero declenche une exception DivideByZeroException

H & H: Research and Training 5 / 30

Page 8: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Capture d’exception

C#

Comment faire pour capturer une exception?

Utiliser un bloc try { ... } catch { ... }

Le try { ... } pour entourer une instruction susceptible dedeclencher une exception

Le catch { ... } pour capturer l’exception et afficher unmessage qui lui correspond

H & H: Research and Training 6 / 30

Page 9: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Capture d’exception

C#Exception : exemple

class Program {static void Main(string[] args)

int x = 5, y = 0;try {

Console.WriteLine(x / y);}catch (DivideByZeroException e) {

Console.WriteLine("Exception : Division par zero ");}Console.WriteLine("Fin de calcul");

}}

Le message affiche a l’execution

Exception : Division par zeroFin de calcul

Constatation

L’exception a ete capturee

Le message Fin de calcul a ete affiche

H & H: Research and Training 7 / 30

Page 10: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Capture d’exception

C#Exception : exemple

class Program {static void Main(string[] args)

int x = 5, y = 0;try {

Console.WriteLine(x / y);}catch (DivideByZeroException e) {

Console.WriteLine("Exception : Division par zero ");}Console.WriteLine("Fin de calcul");

}}

Le message affiche a l’execution

Exception : Division par zeroFin de calcul

Constatation

L’exception a ete capturee

Le message Fin de calcul a ete affiche

H & H: Research and Training 7 / 30

Page 11: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Capture d’exception

C#Exception : exemple

class Program {static void Main(string[] args)

int x = 5, y = 0;try {

Console.WriteLine(x / y);}catch (DivideByZeroException e) {

Console.WriteLine("Exception : Division par zero ");}Console.WriteLine("Fin de calcul");

}}

Le message affiche a l’execution

Exception : Division par zeroFin de calcul

Constatation

L’exception a ete capturee

Le message Fin de calcul a ete affiche

H & H: Research and Training 7 / 30

Page 12: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Capture d’exception

C#Et si je ne connais pas le type d’exception

class Program {static void Main(string[] args)

int x = 5, y = 0;try {

Console.WriteLine(x / y);}catch (Exception e) {

Console.WriteLine("Exception : Division par zero ");}Console.WriteLine("Fin de calcul");

}}

Le meme message sera affiche

Exception : Division par zeroFin de calcul

Constatation

La classe Exception peut etre utilisee

H & H: Research and Training 8 / 30

Page 13: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Capture d’exception

C#Et si je ne connais pas le type d’exception

class Program {static void Main(string[] args)

int x = 5, y = 0;try {

Console.WriteLine(x / y);}catch (Exception e) {

Console.WriteLine("Exception : Division par zero ");}Console.WriteLine("Fin de calcul");

}}

Le meme message sera affiche

Exception : Division par zeroFin de calcul

Constatation

La classe Exception peut etre utilisee

H & H: Research and Training 8 / 30

Page 14: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Capture d’exception

C#Et si je ne connais pas le type d’exception

class Program {static void Main(string[] args)

int x = 5, y = 0;try {

Console.WriteLine(x / y);}catch (Exception e) {

Console.WriteLine("Exception : Division par zero ");}Console.WriteLine("Fin de calcul");

}}

Le meme message sera affiche

Exception : Division par zeroFin de calcul

Constatation

La classe Exception peut etre utilisee

H & H: Research and Training 8 / 30

Page 15: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Capture d’exception

C#

Utiliser des methodes de la classe Exception

class Program {static void Main(string[] args)int x = 5, y = 0;try {Console.WriteLine(x / y);

}catch (DivideByZeroException e) {Console.WriteLine("Exception : " + e.Message);

}Console.WriteLine("Fin de calcul");

}}

Le message affiche

Exception : Tentative de division par zero.Fin de calcul

H & H: Research and Training 9 / 30

Page 16: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Capture d’exception

C#

Utiliser des methodes de la classe Exception

class Program {static void Main(string[] args)int x = 5, y = 0;try {Console.WriteLine(x / y);

}catch (DivideByZeroException e) {Console.WriteLine("Exception : " + e.Message);

}Console.WriteLine("Fin de calcul");

}}

Le message affiche

Exception : Tentative de division par zero.Fin de calcul

H & H: Research and Training 9 / 30

Page 17: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Capture d’exception

C#Utiliser des methodes de la classe Exception

class Program {static void Main(string[] args)int x = 5, y = 0;try {Console.WriteLine(x / y);

}catch (DivideByZeroException e) {Console.WriteLine(e.StackTrace);;

}Console.WriteLine("Fin de calcul");

}}

Le message affiche est :

a MyProject.Program.Main(String[] args) dansC :/.../source/repos/MySolution/MyProject/Program.cs :ligne 43Fin de calcul

H & H: Research and Training 10 / 30

Page 18: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Capture d’exception

C#Utiliser des methodes de la classe Exception

class Program {static void Main(string[] args)int x = 5, y = 0;try {Console.WriteLine(x / y);

}catch (DivideByZeroException e) {Console.WriteLine(e.StackTrace);;

}Console.WriteLine("Fin de calcul");

}}

Le message affiche est :

a MyProject.Program.Main(String[] args) dansC :/.../source/repos/MySolution/MyProject/Program.cs :ligne 43Fin de calcul

H & H: Research and Training 10 / 30

Page 19: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Les exceptions personnalisees

C#

On a utilise (ou vu) des exceptions predefinies

Exception

DivideByZeroException

IndexOutOfRangeException

On peut aussi definir nos exceptions personnalisees

H & H: Research and Training 11 / 30

Page 20: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Les exceptions personnalisees

C#

On a utilise (ou vu) des exceptions predefinies

Exception

DivideByZeroException

IndexOutOfRangeException

On peut aussi definir nos exceptions personnalisees

H & H: Research and Training 11 / 30

Page 21: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Les exceptions personnalisees

C#

La classe Adresse

public class Adresse {

public string Rue { get; set; }public string CodePostal { get; set; }public string Ville { get; set; }

public Adresse(string rue, string ville, stringcodePostal) {Rue = rue;Ville = ville;CodePostal = codePostal;

}}

H & H: Research and Training 12 / 30

Page 22: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Les exceptions personnalisees

C#

Supposons que

codePostal doit contenir exactement 5 chiffres

Demarche a faire

Creer notre propre exception (qui doit etendre la classeException)

Dans le constructeur de Adresse, on lance une exception sicodePostal ne contient pas 5 chiffres

H & H: Research and Training 13 / 30

Page 23: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Les exceptions personnalisees

C#

Supposons que

codePostal doit contenir exactement 5 chiffres

Demarche a faire

Creer notre propre exception (qui doit etendre la classeException)

Dans le constructeur de Adresse, on lance une exception sicodePostal ne contient pas 5 chiffres

H & H: Research and Training 13 / 30

Page 24: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Les exceptions personnalisees

C#

Creons l’exception IncorrectCodePostalException

public class IncorrectCodePostalException :Exception

{// le constructeur de cette nouvelle exceptionpublic IncorrectCodePostalException() :

base("Le code postal doit contenirexactement 5 chiffres")

{}

}

H & H: Research and Training 14 / 30

Page 25: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Les exceptions personnalisees

C#Modifions le constructeur de la classe Adresse

public class Adresse {public string Rue { get; set; }public string CodePostal { get; set; }public string Ville { get; set; }

public Adresse(string rue, string ville, stringcodePostal) {if (codePostal.Length != 5)throw new IncorrectCodePostalException();

Rue = rue;Ville = ville;CodePostal = codePostal;

}}// il faut faire pareil pour le setter du codePostal

H & H: Research and Training 15 / 30

Page 26: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Les exceptions personnalisees

C#

Testons tout cela dans le Main()

static void Main(String[] args) {Adresse a = null;try {a = new Adresse ("paradis", "Marseille", "1300")

;}catch(IncorrectCodePostalException icpe) {Console.WriteLine(icpe.Message);

}}

Le message affiche est :

Le code postal doit contenir exactement 5 chiffres

H & H: Research and Training 16 / 30

Page 27: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Les exceptions personnalisees

C#

Testons tout cela dans le Main()

static void Main(String[] args) {Adresse a = null;try {a = new Adresse ("paradis", "Marseille", "1300")

;}catch(IncorrectCodePostalException icpe) {Console.WriteLine(icpe.Message);

}}

Le message affiche est :

Le code postal doit contenir exactement 5 chiffres

H & H: Research and Training 16 / 30

Page 28: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Les instructions multi-catch

C#

On peut rajouter une deuxieme condition

codePostal doit contenir exactement 5 chiffres

rue doit etre une chaıne en majuscule

H & H: Research and Training 17 / 30

Page 29: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Les instructions multi-catch

C#

Creons une deuxieme exceptionIncorrectStreetNameException

public class IncorrectStreetNameException :Exception

{public IncorrectStreetNameException(): base("Le

nom de la rue doit etre en majuscule"){}

}

H & H: Research and Training 18 / 30

Page 30: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Les instructions multi-catch

C#

Modifions le constructeur de la classe Adresse

public class Adresse {

public string Rue { get; set; }public string CodePostal { get; set; }public string Ville { get; set; }

public Adresse(string rue, string ville, string codePostal) {if (codePostal.Length != 5)throw new IncorrectCodePostalException();

if (!rue.Equals(rue.ToUpper()))throw new IncorrectStreetNameException();

Rue = rue;Ville = ville;CodePostal = codePostal;

}}

H & H: Research and Training 19 / 30

Page 31: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Les instructions multi-catch

C#Re-testons tout cela dans le Main()

static void Main(String[] args){try{

Adresse a = new Adresse ("paradis", "Marseille", "13000");

}catch(IncorrectCodePostalException icpe){

Console.WriteLine(icpe.Message);}catch(IncorrectStreetNameException isne){

Console.WriteLine(isne.Message);}

}

H & H: Research and Training 20 / 30

Page 32: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Les instructions multi-catch

C#

On peut aussi fusionner les catch

static void Main(String[] args){try{Adresse a = new Adresse ("paradis", "Marseille",

"1300");}catch (Exception e) when(e is IncorrectCodePostalException ||e is IncorrectStreetNameException)

{Console.WriteLine(e.Message);

}}

H & H: Research and Training 21 / 30

Page 33: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Les exceptions parametrees

C#

Hypothese

Si on voulait afficher les valeurs qui ont declenche l’exceptiondans le message

H & H: Research and Training 22 / 30

Page 34: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Les exceptions parametrees

C#

Modifions la premiere exceptionIncorrectCodePostalException

public class IncorrectCodePostalException :Exception

{// le constructeur de cette nouvelle exceptionpublic IncorrectCodePostalException(string cp) :

base($"Le code postal { cp } doitcontenir exactement 5 chiffres")

{}

}

H & H: Research and Training 23 / 30

Page 35: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Les exceptions parametrees

C#

Modifions la deuxieme exceptionIncorrectStreetNameException

public class IncorrectStreetNameException :Exception {public IncorrectStreetNameException(String rue) :base ("Le nom de la rue ’" + rue + "’ doit etre

en majuscule"){}

}

H & H: Research and Training 24 / 30

Page 36: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Les exceptions parametrees

C#

Modifions le constructeur de la classe Adresse

public class Adresse {

public string Rue { get; set; }public string CodePostal { get; set; }public string Ville { get; set; }

public Adresse(string rue, string ville, string codePostal) {if (codePostal.Length != 5)throw new IncorrectCodePostalException(codePostal);

if (!rue.Equals(rue.ToUpper()))throw new IncorrectStreetNameException(rue);

Rue = rue;Ville = ville;CodePostal = codePostal;

}}

H & H: Research and Training 25 / 30

Page 37: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Les exceptions parametrees

C#

Pour tester

static void Main(String[] args){try{Adresse a = new Adresse ("paradis", "Marseille", "1300");

}catch (Exception e) when(e is IncorrectCodePostalException ||e is IncorrectStreetNameException)

{Console.WriteLine(e.Message);

}}

Le message affiche est :

Le code postal ’1300’ doit contenir exactement 5 chiffres

H & H: Research and Training 26 / 30

Page 38: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Les exceptions parametrees

C#

Pour tester

static void Main(String[] args){try{Adresse a = new Adresse ("paradis", "Marseille", "1300");

}catch (Exception e) when(e is IncorrectCodePostalException ||e is IncorrectStreetNameException)

{Console.WriteLine(e.Message);

}}

Le message affiche est :

Le code postal ’1300’ doit contenir exactement 5 chiffres

H & H: Research and Training 26 / 30

Page 39: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Les exceptions parametrees

C#

Exercice

Creer une nouvelle classe d’exception AdresseException pourfusionner et remplacer les deux exceptionsIncorrectCodePostalException etIncorrectStreetNameException

H & H: Research and Training 27 / 30

Page 40: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Le bloc finally

C#

A utiliser lorsqu’on a une instruction a executer qu’une exception soitlevee ou non

H & H: Research and Training 28 / 30

Page 41: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Le bloc finally

C#Exemple

public class Program {static void Main(String[] args) {int x = 5, y = 0;try{Console.WriteLine(x/y);

}catch (Exception e){Console.WriteLine("Division par zero");

}finally{Console.WriteLine("Instruction executee systematiquement"

);}

}}

H & H: Research and Training 29 / 30

Page 42: C# : exceptions...Plan 1 Introduction 2 Capture d’exception 3 Les exceptions personnalisees´ 4 Les instructions multi-catch 5 Les exceptions parametr´ ees´ 6 Le bloc finally H

© Achref EL MOUELHI ©

Le bloc finally

C#

Remarque

Le bloc finally peut s’averer interessant si le catch contient unreturn qui forcera l’arret de l’execution du code. Ce bloc (finally)sera toujours execute.

H & H: Research and Training 30 / 30