logo

Java detecta múltiples excepciones

Bloque de captura múltiple de Java

Un bloque try puede ir seguido de uno o más bloques catch. Cada bloque catch debe contener un controlador de excepciones diferente. Por lo tanto, si tiene que realizar diferentes tareas cuando ocurren diferentes excepciones, use el bloque de captura múltiple de Java.

Puntos para recordar

  • A la vez solo ocurre una excepción y a la vez solo se ejecuta un bloque catch.
  • Todos los bloques catch deben ordenarse del más específico al más general, es decir, el catch para ArithmeticException debe ir antes que el catch para Exception.

Diagrama de flujo del bloque de capturas múltiples

Java detecta múltiples excepciones

Ejemplo 1

Veamos un ejemplo sencillo de bloque multicaptura de Java.

MultipleCatchBlock1.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'); } } 
Pruébalo ahora

Producción:

¿Cuál es el tamaño de la pantalla de mi monitor?
 Arithmetic Exception occurs rest of the code 

Ejemplo 2

MultipleCatchBlock2.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'); } } 
Pruébalo ahora

Producción:

 ArrayIndexOutOfBounds Exception occurs rest of the code 

En este ejemplo, el bloque try contiene dos excepciones. Pero a la vez sólo ocurre una excepción y se ejecuta su bloque catch correspondiente.

MultipleCatchBlock3.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'); } } 
Pruébalo ahora

Producción:

 Arithmetic Exception occurs rest of the code 

Ejemplo 4

En este ejemplo, generamos NullPointerException, pero no proporcionamos el tipo de excepción correspondiente. En tal caso, el bloque catch que contiene la clase de excepción principal Excepción voluntad invocada.

MultipleCatchBlock4.java

¿Qué es un sistema de archivos Linux?
 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'); } } 
Pruébalo ahora

Producción:

 Parent Exception occurs rest of the code 

Ejemplo 5

Veamos un ejemplo, para manejar la excepción sin mantener el orden de las excepciones (es decir, de la más específica a la más general).

MultipleCatchBlock5.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...'); } } 
Pruébalo ahora

Producción:

 Compile-time error