OCA JSE8 - Exceptions

package com.codemonkey; import static java.lang.System.err; import static java.lang.System.out; //Estas importaciones no son necesarias: import java.lang.Exception;//subclase de java.lang.Throwable import java.lang.RuntimeException;//subclase de Exception import java.lang.ArithmeticException;//subclase de RuntimeException import java.lang.IllegalArgumentException;//subclase de RuntimeException import java.lang.NumberFormatException;//subclase de IllegalArgumentException import java.lang.IndexOutOfBoundsException;//subclase de RuntimeException import java.lang.ArrayIndexOutOfBoundsException;//subclase de IndexOutOfBoundsException import java.lang.StringIndexOutOfBoundsException;//subclase de IndexOutOfBoundsException import java.lang.NullPointerException;//subclase de RuntimeException import java.lang.ClassCastException;//sublcase de RuntimeException import java.util.ArrayList; //Esta si es necesaria: import java.io.IOException;//subclase de Exception public class Main{ public static void main(String[] args){ //Para operaciones invalidas, ej. Division x cero ArithmeticExceptionDemo(); //Para formatos de numero no validos, ej. Integer.parseInt("12B") NumberFormatExceptionDemo(); //Cuando se trata de acceder a indices inexistentes de un arreglo ArrayIndexOutOfBoundsExceptionDemo(); //Para un Cast mal realizado ClassCastExceptionDemo(); //Cuando hay objeto null y se trata de operar sobre el NullPointerExceptionDemo(); //Cuando se excede del rango de indices en un String StringIndexOutOfBoundsExceptionDemo(); //Metodo interrumpido InterruptedExceptionDemo(); } public static void InterruptedExceptionDemo() throws InterruptedException{ try{ Main.metodoInterrumpido(); out.println("A"); }catch(Exception e){ e.printStackTrace();//java.lang.InterruptedException: El tiempo ha terminado. } finally{ out.println("B");//B } out.println("C");// } static void metodoInterrumpido() throws InterruptedException{ throw new InterruptedException("El tiempo ha terminado."); } public static void StringIndexOutOfBoundsExceptionDemo(){ String str = "Ingenieria quimica.";//Tam=19, [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18] out.println("Tam:"+str.length());//19 //Ingenieria quimica. [*0,1,2,3,4,5,6,7,8,9,*10,11,12,13,14,15,16,17,18] // | I n g e n i e r i a | q u i m i c a . String strPart1 = str.substring(0,10); out.println("strPart1 : " +strPart1);//Ingenieria try{ String strPart2 = str.substring(14,36); out.println("strPart2 : " +strPart2); }catch(StringIndexOutOfBoundsException | NullPointerException e11){ err.println("Tratas de acceder a un indice no existente:"+e11.getMessage()); e11.printStackTrace(); }catch(Exception e12){ err.println("Exception generica!!"); } } public static void NullPointerExceptionDemo(){ String str = null; try{ str = str.concat(" "+ new String("Null")); out.printf("%s\n",str); }catch(ArrayIndexOutOfBoundsException | NullPointerException e10){ err.println("Un error al operar con objeto null:"+e10.getMessage()); e10.printStackTrace(); } } public static void ClassCastExceptionDemo(){ ArrayList listaNumeros = new ArrayList(); listaNumeros.add(0,"2"); listaNumeros.add(1,"3"); listaNumeros.add(2,"7"); listaNumeros.add(3,"9"); try{ //Son numeros en formato String, no Integer Integer miNumero = (Integer) listaNumeros.get(0); out.println(miNumero); }catch(ClassCastException e7){ err.println("Un error en el Cast:"+e7.getMessage()); e7.printStackTrace(); } catch(RuntimeException e8){ err.println("Exception en tiempo de ejecucion"); } catch(Exception e9){ err.println("Exception no especificada"); } } public static void ArrayIndexOutOfBoundsExceptionDemo(){ int[] arreglo = new int[3];//[0,1,2] try{ arreglo[0]=5; arreglo[1]=-2; arreglo[2]=0; arreglo[3]=12;//Indice no existente }catch(IndexOutOfBoundsException | NullPointerException e4){ //ArrayIndexOutOfBoundsException es subclase //de IndexOutOfBoundsException err.println("Ha ocurrido una Exception, un indice inexistente:"+e4.getMessage()); e4.printStackTrace(); }catch(RuntimeException e5){ err.println("Ha ocirrido una Exception en tiempo de ejecucion!!"); }catch(Exception e6){ err.println("Una exception no especificada!!"); } } public static void NumberFormatExceptionDemo(){ try{ //formato incorrecto "23.00G" double numero = Double.parseDouble("23.00G"); out.println(numero); }catch(IllegalArgumentException | ArrayIndexOutOfBoundsException e2){ //NumberFormatException es una subclase de IllegalArgumentException err.println("Ha ocurrido una Exception:"+e2.getMessage()); e2.printStackTrace(); }catch(Exception e3){ err.println("Ha ocurrido una Exception no especificada:"+e3.getMessage()); } } public static void ArithmeticExceptionDemo(){ try{ //No se puede dividir un numero entre 0 int division = 3 / 0; out.printf("%d",division); }catch(ArithmeticException | ArrayIndexOutOfBoundsException e1){ err.println("Ha ocurrido una Exception:"+e1.getMessage()); e1.printStackTrace(); } } }

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.