Java-uitsonderings - Probeer ... Vang
Java-uitsonderings
Wanneer Java-kode uitgevoer word, kan verskillende foute voorkom: koderingsfoute wat deur die programmeerder gemaak is, foute as gevolg van verkeerde invoer, of ander onvoorsienbare dinge.
Wanneer 'n fout voorkom, sal Java normaalweg stop en 'n foutboodskap genereer. Die tegniese term hiervoor is: Java sal 'n uitsondering gooi (gooi 'n fout).
Java probeer vang
Die try
stelling laat jou toe om 'n blok kode te definieer wat vir foute getoets moet word terwyl dit uitgevoer word.
Die catch
stelling laat jou toe om 'n blok kode te definieer wat uitgevoer moet word, as 'n fout in die probeerblok voorkom.
Die try
en catch
sleutelwoorde kom in pare:
Sintaksis
try {
// Block of code to try
}
catch(Exception e) {
// Block of code to handle errors
}
Oorweeg die volgende voorbeeld:
Dit sal 'n fout genereer, want myNumbers[10] bestaan nie.
public class Main {
public static void main(String[ ] args) {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]); // error!
}
}
Die uitset sal iets soos volg wees:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at Main.main(Main.java:4)
As 'n fout voorkom, kan ons try...catch
die fout opspoor en 'n kode uitvoer om dit te hanteer:
Voorbeeld
public class Main {
public static void main(String[ ] args) {
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Something went wrong.");
}
}
}
Die uitset sal wees:
Something went wrong.
Uiteindelik
Die finally
stelling laat jou kode uitvoer, na try...catch
, ongeag die resultaat:
Voorbeeld
public class Main {
public static void main(String[] args) {
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Something went wrong.");
} finally {
System.out.println("The 'try catch' is finished.");
}
}
}
Die uitset sal wees:
Something went wrong.
The 'try catch' is finished.
Die gooi sleutelwoord
Die throw
stelling laat jou toe om 'n persoonlike fout te skep.
Die throw
stelling word saam met 'n uitsonderingstipe gebruik . Daar is baie uitsonderingstipes beskikbaar in Java: ArithmeticException
,
FileNotFoundException
, ArrayIndexOutOfBoundsException
, SecurityException
, ens:
Voorbeeld
Gooi 'n uitsondering as ouderdom onder 18 is (druk "Toegang geweier"). As ouderdom 18 of ouer is, druk "Toegang verleen":
public class Main {
static void checkAge(int age) {
if (age < 18) {
throw new ArithmeticException("Access denied - You must be at least 18 years old.");
}
else {
System.out.println("Access granted - You are old enough!");
}
}
public static void main(String[] args) {
checkAge(15); // Set age to 15 (which is below 18...)
}
}
Die uitset sal wees:
Exception in thread "main" java.lang.ArithmeticException: Access denied - You must be at least 18 years old.
at Main.checkAge(Main.java:4)
at Main.main(Main.java:12)
As ouderdom 20 was, sou jy nie 'n uitsondering kry nie:
Voorbeeld
checkAge(20);
Die uitset sal wees:
Access granted - You are old enough!