Lanzar excepciones en Java

package com.codemonkey; import static java.lang.System.out; import static java.lang.System.err; public class LarzarExcepciones{ public static void main(String[] args){ out.println("\t---Lanzamiento de Excepciones ---\n"); try{ throw new NumberFormatException(); }catch(IllegalArgumentException iae){ iae.printStackTrace(); } //Salida: java.lang.NumberFormatException out.println(); try{ throw new ArrayIndexOutOfBoundsException(); }catch(IndexOutOfBoundsException ioe){ ioe.printStackTrace(); }//Salida: java.lang.ArrayIndexOutOfBoundsException out.println(); try{ throw new ArithmeticException(); }catch(RuntimeException re){ err.println("Ha ocurrido un java.lang.ArithmeticException"); }//Salida: Ha ocurrido un java.lang.ArithmeticException out.println(); try{ throw new ClassCastException(); }catch(RuntimeException re){ err.println("Ha ocurrido un java.lang.ClassCastException"); }//Salida: Ha ocurrido un java.lang.ClassCastException //Esto es valido try{ out.println("try"); }finally{ out.println("finally"); }//Salida: try finally } }
Ejemplo de lanzamiento de excepciones

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.