tp10
This commit is contained in:
6
DEV2.1/Exception/ex1/NullPointerException.java
Normal file
6
DEV2.1/Exception/ex1/NullPointerException.java
Normal file
@@ -0,0 +1,6 @@
|
||||
public class NullPointerException {
|
||||
public static void main(String[] args) {
|
||||
String str = null;
|
||||
int length = str.length(); // Tentative d'accéder à une méthode sur un objet nul
|
||||
}
|
||||
}
|
||||
6
DEV2.1/Exception/ex1/NumberFormatException.java
Normal file
6
DEV2.1/Exception/ex1/NumberFormatException.java
Normal file
@@ -0,0 +1,6 @@
|
||||
public class NumberFormatException {
|
||||
public static void main(String[] args) {
|
||||
String str = "abc";
|
||||
int num = Integer.parseInt(str); // Conversion d'une chaîne non numérique en entier
|
||||
}
|
||||
}
|
||||
5
DEV2.1/Exception/ex1/RuntimeExceptionExample.java
Normal file
5
DEV2.1/Exception/ex1/RuntimeExceptionExample.java
Normal file
@@ -0,0 +1,5 @@
|
||||
public class RuntimeExceptionExample {
|
||||
public static void main(String[] args) {
|
||||
throw new UnsupportedOperationException("Operation not supported"); // Lancement d'une exception d'opération non supportée
|
||||
}
|
||||
}
|
||||
14
DEV2.1/Exception/ex2/ArithmeticExceptionExample.java
Normal file
14
DEV2.1/Exception/ex2/ArithmeticExceptionExample.java
Normal file
@@ -0,0 +1,14 @@
|
||||
public class ArithmeticExceptionExample {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
computeDivision(); // Appel de la méthode responsable de l'exception
|
||||
} catch (ArithmeticException e) {
|
||||
System.out.println("Une erreur arithmétique s'est produite : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// Méthode responsable de l'exception
|
||||
public static void computeDivision() {
|
||||
int result = 10 / 0; // Division par zéro
|
||||
}
|
||||
}
|
||||
9
DEV2.1/Exception/ex2/Main.java
Normal file
9
DEV2.1/Exception/ex2/Main.java
Normal file
@@ -0,0 +1,9 @@
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
ArithmeticExceptionVrai.computeDivision();
|
||||
} catch (ArithmeticException e) {
|
||||
System.out.println("Une erreur arithmétique s'est produite : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
13
DEV2.1/Exception/ex3/Incomplet.java
Normal file
13
DEV2.1/Exception/ex3/Incomplet.java
Normal file
@@ -0,0 +1,13 @@
|
||||
import java.io.IOException;
|
||||
|
||||
public class Incomplet {
|
||||
public static void main(String[] args) {
|
||||
byte[] txt = {0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x0D, 0x0A};
|
||||
try {
|
||||
System.out.write(txt);
|
||||
System.out.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user