Gegevenstypen en uitzonderingen in Java
Jim White
Java Developer
public static void main(String[] args) {
ArrayList<String> allStars = new ArrayList<String>();
allStars.add("Mays");
allStars.add("Aaron");
allStars.add("Ruth");
String last = allStars.get(3); // This will cause an exception
System.out.println(last);
}
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 3 out of
bounds for length 3
try {
// Statements om uit te voeren en op excepties te letten
}
try {
// Statements om uit te voeren en op excepties te letten
} catch (<Exception type> <variable name>) {
// Statements bij een opgetreden exceptie
}
try {
ArrayList<String> allStars = new ArrayList<String>();
allStars.add("Mays");
allStars.add("Aaron");
allStars.add("Ruth");
String last = allStars.get(3); // Accessing an element that isn't there
System.out.println(last);
} catch (IndexOutOfBoundsException e) {
System.out.println("Oeps - verkeerde index");
}
Oeps - verkeerde index
catch-blokken voor verschillende exceptiescatch heeft een eigen exceptietype en variabelecatch-blokken op volgorde tot er een match istry {
ArrayList<String> nums
= new ArrayList<String>();
nums.add("one");
String last = nums.get(0);
Integer.valueOf(last);
System.out.println(last);
} catch (IndexOutOfBoundsException eIndex) {
System.out.println("Oeps - verkeerde index");
} catch (NumberFormatException eValueOf) {
System.out.println(
"Oeps - String is geen getal");
}
Oeps - String is geen getal
catch (Exception variable){ } als vangnettry {
// Code om te proberen
} catch (IndexOutOfBoundsException eIndex) {
System.out.println("Oeps - verkeerde index");
} catch (NumberFormatException eValueOf) {
System.out.println("Oeps - String is geen getal");
} catch (Exception e) { // Handelt alles af behalve de twee hierboven
System.out.println("Er gebeurde iets onverwachts");
}
finally-blok voert altijd uit// Als er een exceptie optreedt
try {
Integer.valueOf("one");
} catch (NumberFormatException eValueOf) {
System.out.println("Oeps");
} finally {
System.out.println("Opschonen");
}
Oeps
Opschonen
// Als er geen exceptie wordt gegooid
try {
Integer.valueOf("1");
} catch (NumberFormatException eValueOf) {
System.out.println("Oeps");
} finally {
System.out.println("Opschonen");
}
Opschonen
Exception-object in de catch is rijk aan info.getMessage() voor de leesbare oorzaak.getClass() voor de exceptieklasse/-typenaam .printStackTrace() om de stacktrace te tonen (volgende slide)public class ExceptionExample {
public static void main(String[] args) {
try {
Integer.valueOf("one");
} catch (NumberFormatException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
For input string: "one"
java.lang.NumberFormatException: For input string: "one"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:661)
at java.base/java.lang.Integer.valueOf(Integer.java:988)
at foo/exceptions.ExceptionExample.main(ExceptionExample.java:7)
Gegevenstypen en uitzonderingen in Java