[Précédent]
[Index] | [Corrigé] | [Version imprimable]
[Prochain]

13.4
  Censure - Enoncé
Niveau 1
 
 
But:
  Donner la trace d'exécution d'une programme (sans l'exécuter)    
Thème:
  exceptions    
Fichiers:
  Censure.java    

Pour cet exercice munissez-vous d'une feuille de papier et d'un crayon.

Soit le programme suivant :

 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
57
58
59
60
61
62
63
64
65
66
// Note : le  message de mise en garde de Eclipse:
// the serializable class NietException does not declare a static
// final serialVersionUID field of type long
// peut etre neglig'e.

class Censure {
    
    public static void main(String[] args) {
        Site s1 = new Site("cowww.epfl.ch/coursOOP/", "Prof d'info");
        Site s2 = new Site("www.PCcrackers.com", "Hackers Inc.");
        try {
            System.out.println("Trying " + s1.getAddress());
            s1.connect();
            System.out.println("Argh !!");
            try {
                System.out.println("Trying " + s2.getAddress());
                s2.connect();
                System.out.println("Woaw !");
            } catch (NietException e) {
                System.out.println(e.getMessage());
                System.out.println("Do you really want to be here ?");
                throw e;
            } finally {
                System.out.println("Finally !");
            }
            System.out.println("Happy end!");
        } catch (NietException e) {
            System.out.println(e.getMessage());
            System.out.println("Is Big brother watching you ?");
        }
        System.out.println("Stop surfing !");
    }
}

class Site {

    private String address;
    private String source;
    
    public Site(String address, String source) {
        this.address = address;
        this.source = source;
    }
    
    public void connect() throws NietException {
        if (source.equals("Hackers Inc.")) {
            throw new NietException();
        } else {
            System.out.println("Connected to " + address);
        }
    }
    
    public String getAddress() {
        return address;
    }
}

class NietException extends Exception {

    public NietException() {}

    @Override
    public String getMessage() {
        return "Illegal move";
    }
}

Indiquez sans exécuter le programme ce qu'il va afficher.

Vérifiez ensuite votre réponse en exécutant le programme.

 


[Précédent]
[Index] | [Corrigé] | [Version imprimable]
[Prochain]