suivant: Classe abstraite
monter: Programmes Java illustrant le
précédent: Programmes Java illustrant le
Le premier programme illustre le fonctionnement d'une exception.
Exception1.java
|
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36
|
//class MonException extends Exception{}
class Exception1{
public static void main(String[] argv){
A a = new A();
a.test(3);
a.test(8);
}
}
class MonException extends Exception{}
class A{
int[] t = new int[5];
void test(int x){
try{
for (int i=0; i<x; i++){
System.out.println("avant "+i);
affect(i,i+x);
System.out.println("après "+i);
}
}catch(MonException me){
System.out.println("on est dans le traitement d'exception");
}
System.out.println("On termine le test");
System.out.println();
}
void affect(int indice, int valeur) throws MonException{
if (indice < t.length){
t[indice]=valeur;
}else{
throw new MonException();
}
}
}
|
Java2html
|
Le programme Exception2
illustre la transmission d'information
utilisant l'objet Exception
levé.
Exception2.java
|
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
|
class Exception2{
public static void main(String[] argv){
Compte c = new Compte();
try{
c.depot(50);
c.print();
c.retrait(75);
c.print();
}catch(ProvisionInsuffisanteException pie){
System.out.println("Provision insuffisante");
System.out.print(pie.demande);
System.out.print(" euros demandes, ");
System.out.print(pie.dispo);
System.out.println(" euros disponibles. ");
}
c.print();
}
}
class ProvisionInsuffisanteException extends Exception{
int demande;
int dispo;
ProvisionInsuffisanteException(int x, int y){
demande=x;
dispo=y;
}
}
class Compte{
private int solde;
void depot(int x) throws ProvisionInsuffisanteException{
if (x<0)
this.retrait(-x);
else
solde = solde+x;
}
void retrait(int x) throws ProvisionInsuffisanteException{
if (x<0)
this.depot(-x);
else{
if(x> solde)
throw new ProvisionInsuffisanteException(x,solde);
else
solde = solde-x;
}
}
void print(){
System.out.println("le solde de ce compte est de "+solde+" euros");
}
}
|
Java2html
|
Le troisième exemple illustre la déroulement d'un programme selon la
levée et la capture d'exception.
Exception3.java
|
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56
|
class Exception3{
public static void main(String[] argv){
A v = new A();
try{
v.cas(1);
v.cas(2);
v.cas(3);
v.cas(4);
}catch(TroisiemeException te){
}
System.out.println("fin de main");
}
}
class PremiereException extends Exception{}
class DeuxiemeException extends Exception{}
class TroisiemeException extends Exception{}
class A{
void cas(int i) throws TroisiemeException {
System.out.println("debut de cas");
try{
instr(1);
instr(2);
risque(i);
instr(4);
instr(5);
}catch(PremiereException me){
instr(6);
instr(7);
}catch(DeuxiemeException me){
instr(8);
instr(9);
}
instr(10);
instr(11);
System.out.println("fin de cas");
System.out.println("---------------------------------");
}
void instr(int i){
System.out.println("instruction "+i);
}
void risque(int x) throws PremiereException,
DeuxiemeException, TroisiemeException{
System.out.println("debut d'instruction risque");
switch(x){
case 1: break;
case 2: throw new PremiereException();
case 3: throw new DeuxiemeException();
case 4: throw new TroisiemeException();
}
System.out.println("fin d'instruction risque");
}
}
|
Java2html
|
Le dernier exemple concernant les exceptions illustre la transmission
d'exception à travers plusieurs niveaux d'envois de messages.
Exception4.java
|
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36
|
class Exception4{
public static void main(String[] argv){
(new A()).go(true);
System.out.println("-----------------------");
(new A()).go(false);
}
}
class MonException extends Exception{}
class A{
void go(boolean b){
try{
System.out.println("debut de go");
m(b);
}catch(MonException me){
}
System.out.println("fin de go");
}
void m(boolean b) throws MonException{
System.out.println("debut de m");
n(b);
System.out.println("fin de m");
}
void n(boolean b) throws MonException{
System.out.println("debut de n");
p(b);
System.out.println("fin de n");
}
void p(boolean b) throws MonException{
System.out.println("debut de p");
if(b)
throw new MonException();
System.out.println("fin de p");
}
}
|
Java2html
|
Barthelemy Francois
2003-03-11