Java'da Veri Türleri ve İstisnalar
Jim White
Java Developer
Exception yakalamak için try/catch kullanın ve sorunu gidermek üzere kod yazınthrows kullanarak istisnayı çağıran metoda iletebilirsinizthrows sonrası istisna türlerini virgülle ayırınpublic static void someMethod() throws IndexOutOfBoundsException,
NegativeArraySizeException, NullPointerException {
// method code
}
Bir Exception için normal try-catch
public static void main(String[] args) {
someMethod();
}
public static void someMethod(){
try {
ArrayList<String> games
= new ArrayList<String>();
games.add("Monopoly");
games.add("Chess");
games.get(3);
} catch (IndexOutOfBoundsException e) {
System.out.println(
"Oops - trying to get non-existent item");
}
}
Bir Exception için throws kullanma
public static void main(String[] args) {
try {
someMethod();
} catch (IndexOutOfBoundsException e) {
System.out.println(
"someMethod tried to get non-existent item");
}
}
public static void someMethod()
throws IndexOutOfBoundsException {
ArrayList<String> games
= new ArrayList<String>();
games.add("Monopoly");
games.add("Chess");
games.get(3);
}

Exception yeniden "fırlatılabilir"main içinden yeniden fırlatmak uygulamayı durdurur
public class RethrowExample {
public static void main(String[] args) {
try {
method1(0);
} catch (ArithmeticException e) {
System.out.println("Oops - tried a bad quotient");
}
}
public static void method1(int divisor) throws ArithmeticException {
method2(divisor);
}
public static void method2 (int divisor) throws ArithmeticException {
int z = 5/divisor;
System.out.println(z);
}
}
Java'da Veri Türleri ve İstisnalar