Java'da İç İçe Try Blokları
Java'da bir try ifadesi başka bir try bloğunun içinde olabilir. İç içe try bloğu olarak adlandırılır. Bir try deyimi her girildiğinde, bu istisnanın içeriği yığına gönderilir.
Neden iç içe try bloğu kullanıyorsunuz?
Bazen bir bloğun bir kısmının bir hataya neden olabileceği ve tüm bloğun kendisinin başka bir hataya neden olabileceği bir durum ortaya çıkabilir. Bu gibi durumlarda, istisna işleyicilerin iç içe olması gerekir.
Sözdizimi
try {
statement 1;
statement 2;
try {
statement 1;
statement 2;
}
catch(Exception e) {
// catch the corresponding exception
}
}
catch(Exception e) {
// catch the corresponding exception
}
.............
// An example of nested try statements.
class NestTry {
public static void main(String args[]) {
try {
int a = args.length;
/* If no command-line args are present,
the following statement will generate
a divide-by-zero exception. */
int b = 42 / a;
System.out.println("a = " + a);
try { // nested try block
/* If one command-line arg is used,
then a divide-by-zero exception
will be generated by the following code. */
if(a==1) a = a/(a-a); // division by zero
/* If two command-line args are used,
then generate an out-of-bounds exception. */
if(a==2) {
int c[] = { 1 };
c[42] = 99; // generate an out-of-bounds exception
}
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Array index out-of-bounds: " + e);
}
} catch(ArithmeticException e) {
System.out.println("Divide by 0: " + e);
}
}
}
Komut satırı yok : Programı hiçbir komut satırı bağımsız değişkeni olmadan yürüttüğünüzde, dış try bloğu tarafından bir sıfıra bölme istisnası oluşturulur.
Bir komut satırı bağımsız değişkeni : Programın bir komut satırı bağımsız değişkeniyle yürütülmesi, iç içe try bloğu içinden bir sıfıra bölme istisnası oluşturur. İç blok bu istisnayı yakalayamadığı için işlendiği dış try bloğuna aktarılır.
İki komut satırı bağımsız değişkeni : Programı iki komut satırı bağımsız değişkeniyle yürütürseniz, iç try bloğu içinden bir dizi sınırı istisnası oluşturulur. Aşağıda, her durumu gösteren örnek çalışmalar verilmiştir:
C:\>java NestTry
Divide by 0: java.lang.ArithmeticException: / by zero
C:\>java NestTry One
a = 1
Divide by 0: java.lang.ArithmeticException: / by zero
C:\>java NestTry One Two
a = 2
Array index out-of-bounds:
java.lang.ArrayIndexOutOfBoundsException:42
Özellikle kurtarma kodunuz bir istisna oluşturabiliyorsa, bu bazen kaçınılmazdır. Güzel değil, ama bazen alternatif yok.
Yan Notlar
- Bir iç try deyiminin belirli bir istisna için eşleşen bir catch deyimi yoksa , kontrol , eşleşen bir catch deyimi için beklenen bir sonraki try deyimi catch işleyicilerine aktarılır .
- Bu, catch deyimlerinden biri başarılı olana veya iç içe try deyimlerinin tümü tamamlanana kadar devam eder.
- Hiçbir catch deyimi eşleşmezse, Java çalışma zamanı sistemi istisnayı işleyecektir. Bir try bloğunun belirli bir istisna için bir catch işleyicisi olmadığı her durumda , o catch bloğunun yürütüldüğü eşleşme bulunursa, o zaman try bloğunun catch blokları söz konusu istisna için denetlenir. Ne catch bloğu ne de ebeveyn catch bloğu istisnayı işlemezse o zaman , istisnayı işlemediğimizde gördüğümüze benzer şekilde, çalışma zamanı sistemi tarafından oluşturulan mesaj istisna için gösterilecektir.
- İç içe try blokları kullanıldığında , önce iç try bloğu yürütülür. İç try bloğunda atılan herhangi bir istisna, karşılık gelen catch bloğunda yakalanır. Eşleşen bir catch bloğu bulunamazsa, dış try bloğunun catch bloğu , iç içe geçmiş tüm try ifadeleri tükenene kadar denetlenir. Eşleşen blok bulunamazsa, yürütmeyi Java Runtime Environment yürütür.
// An example of nested try statements.
class NestTry {
public static void main(String args[]) {
try {
int a = args.length;
/* If no command-line args are present,
the following statement will generate
a divide-by-zero exception. */
int b = 42 / a;
System.out.println("a = " + a);
try { // nested try block
/* If one command-line arg is used,
then a divide-by-zero exception
will be generated by the following code. */
if(a==1) a = a/(a-a); // division by zero
/* If two command-line args are used,
then generate an out-of-bounds exception. */
if(a==2) {
int c[] = { 1 };
c[42] = 99; // generate an out-of-bounds exception
}
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Array index out-of-bounds: " + e);
}
} catch(ArithmeticException e) {
System.out.println("Divide by 0: " + e);
}
}
}
C:\>java NestTry
Divide by 0: java.lang.ArithmeticException: / by zero
C:\>java NestTry One
a = 1
Divide by 0: java.lang.ArithmeticException: / by zero
C:\>java NestTry One Two
a = 2
Array index out-of-bounds:
java.lang.ArrayIndexOutOfBoundsException:42
/* Try statements can be implicitly nested via
calls to methods. */
class MethNestTry {
static void nesttry(int a) {
try { // nested try block
/* If one command-line arg is used,
then a divide-by-zero exception
will be generated by the following code. */
if(a==1) a = a/(a-a); // division by zero
/* If two command-line args are used,
then generate an out-of-bounds exception. */
if(a==2) {
int c[] = { 1 };
c[42] = 99; // generate an out-of-bounds exception
}
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Array index out-of-bounds: " + e);
}
}
public static void main(String args[]) {
try {
int a = args.length;
/* If no command-line args are present,
the following statement will generate
a divide-by-zero exception. */
int b = 42 / a;
System.out.println("a = " + a);
nesttry(a);
} catch(ArithmeticException e) {
System.out.println("Divide by 0: " + e);
}
}
}
Okuduğunuz için teşekkürler. İyi öğrenmeler
Mouad Oumous Java WhatsApp Grubuna Katılın
Mouad Oumous Telegram Kanalına Katılın
Yayınımızı takip ederek destek olun

![Bağlantılı Liste Nedir? [Bölüm 1]](https://post.nghiatu.com/assets/images/m/max/724/1*Xokk6XOjWyIGCBujkJsCzQ.jpeg)



































