جافا التقاط استثناءات متعددة

جافا التقاط استثناءات متعددة

كتلة جافا متعددة الالتقاط

يمكن أن يتبع كتلة المحاولة كتلة التقاط واحدة أو أكثر. يجب أن تحتوي كل كتلة التقاط على معالج استثناء مختلف. لذلك، إذا كان عليك تنفيذ مهام مختلفة عند حدوث استثناءات مختلفة، فاستخدم كتلة Java Multi-Catch.

نقطة لنتذكر

  • في كل مرة يحدث استثناء واحد فقط وفي كل مرة يتم تنفيذ كتلة التقاط واحدة فقط.
  • يجب ترتيب جميع كتل الالتقاط من الأكثر تحديدًا إلى الأكثر عمومية، أي أن الالتقاط للاستثناء الحسابي يجب أن يأتي قبل الالتقاط للاستثناء.

مخطط انسيابي لكتلة الالتقاط المتعدد

جافا قبض على استثناءات متعددة

مثال 1

دعونا نرى مثالاً بسيطًا على كتلة Java multi-catch.

MultiCatchBlock1.java

 public class MultipleCatchBlock1 { public static void main(String[] args) { try{ int a[]=new int[5]; a[5]=30/0; } catch(ArithmeticException e) { System.out.println('Arithmetic Exception occurs'); } catch(ArrayIndexOutOfBoundsException e) { System.out.println('ArrayIndexOutOfBounds Exception occurs'); } catch(Exception e) { System.out.println('Parent Exception occurs'); } System.out.println('rest of the code'); } }  
اختبره الآن

انتاج:

 Arithmetic Exception occurs rest of the code  

مثال 2

MultiCatchBlock2.java

 public class MultipleCatchBlock2 { public static void main(String[] args) { try{ int a[]=new int[5]; System.out.println(a[10]); } catch(ArithmeticException e) { System.out.println('Arithmetic Exception occurs'); } catch(ArrayIndexOutOfBoundsException e) { System.out.println('ArrayIndexOutOfBounds Exception occurs'); } catch(Exception e) { System.out.println('Parent Exception occurs'); } System.out.println('rest of the code'); } }  
اختبره الآن

انتاج:

 ArrayIndexOutOfBounds Exception occurs rest of the code  

في هذا المثال، تحتوي كتلة المحاولة على استثناءين. ولكن في كل مرة يحدث استثناء واحد فقط ويتم تنفيذ كتلة الالتقاط المقابلة لها.

MultiCatchBlock3.java

 public class MultipleCatchBlock3 { public static void main(String[] args) { try{ int a[]=new int[5]; a[5]=30/0; System.out.println(a[10]); } catch(ArithmeticException e) { System.out.println('Arithmetic Exception occurs'); } catch(ArrayIndexOutOfBoundsException e) { System.out.println('ArrayIndexOutOfBounds Exception occurs'); } catch(Exception e) { System.out.println('Parent Exception occurs'); } System.out.println('rest of the code'); } }  
اختبره الآن

انتاج:

 Arithmetic Exception occurs rest of the code  

مثال 4

في هذا المثال، قمنا بإنشاء NullPointerException، لكننا لم نوفر نوع الاستثناء المقابل. في مثل هذه الحالة، تحتوي كتلة الالتقاط على فئة الاستثناء الأصل استثناء سيتم الاحتجاج.

MultiCatchBlock4.java

 public class MultipleCatchBlock4 { public static void main(String[] args) { try{ String s=null; System.out.println(s.length()); } catch(ArithmeticException e) { System.out.println('Arithmetic Exception occurs'); } catch(ArrayIndexOutOfBoundsException e) { System.out.println('ArrayIndexOutOfBounds Exception occurs'); } catch(Exception e) { System.out.println('Parent Exception occurs'); } System.out.println('rest of the code'); } }  
اختبره الآن

انتاج:

 Parent Exception occurs rest of the code  

مثال 5

دعونا نرى مثالاً، للتعامل مع الاستثناء دون الحفاظ على ترتيب الاستثناءات (أي من الأكثر تحديدًا إلى الأكثر عمومية).

MultiCatchBlock5.java

 class MultipleCatchBlock5{ public static void main(String args[]){ try{ int a[]=new int[5]; a[5]=30/0; } catch(Exception e){System.out.println('common task completed');} catch(ArithmeticException e){System.out.println('task1 is completed');} catch(ArrayIndexOutOfBoundsException e){System.out.println('task 2 completed');} System.out.println('rest of the code...'); } }  
اختبره الآن

انتاج:

 Compile-time error