Académique Documents
Professionnel Documents
Culture Documents
E/S
Problèmes des erreurs dans les programmes
2
2 familles d’exceptions:
non fatales
fatales (provoquent l’arrêt du programme).
Hiérarchie des exceptions
6
Object
Throwable
Exception
Error
RunTimeException IOException
…
7
Exemples
NoSuchMethodError: la méthode référencée n’est pas
accessible
StackOverflowError: débordement de pile
Hiérarchie RuntimeException
9
Exemples
ArithmeticException: une erreur arithmétique (division
par 0…)
IndexOutofBoundsException: indice d’un tableau est en
dehors des bornes autorisées.
Il n’est pas obligatoire de gérer les exceptions de
type RuntimeException
10
Instruction i ;
Instruction i
} catch (Exception ex) {
2 Instruction j;
}
3 Instruction k;
Finally pour ce qui s’exécute dans tous
15
les cas
Try { Try {
Instruction 1; Instruction 1;
Instruction i; Instruction i;
} catch (Exception ex) { Instruction k;
Instruction j; } catch (Exception ex) {
} finally { Instruction j;
Instruction k; Instruction k;
} }
Remarque: si le bloc try/catch a une instruction return finally s’exécute quand même
Le flot saute à finally puis revient à return
Extrait de la documentation de la
16
classe read
Method Detail read
public abstract int read() throws IOException
Reads the next byte of data from the input stream. The value byte is
returned as an int in the range 0 to 255. If no byte is available because
the end of the stream has been reached, the value -1 is returned. This
method blocks until input data is available, the end of the stream is
detected, or an exception is thrown. A subclass must provide an
implementation of this method.
Returns:
the next byte of data, or -1 if the end of the stream is reached.
Throws:
IOException - if an I/O error occurs.
Deux manières de gérer les exceptions:
la prendre en compte ou l’esquiver
17
import java.io.IOException;
public class TaperTouche
{
static public void main(String[] args)
{
System.out.println("Tapez une touche pour terminer le programme");
try
{
System.in.read();
}
catch(IOException e)
{
System.out.println("Une IOException a été détecté !");
}
}
}
L’esquiver
18 Il s’agit de déclarer que vous lancez l’exception même si
techniquement ce n’est pas vous qui la lancez