Você está na página 1de 3

package excepciones1;

import java.util.InputMismatchException;
import java.util.Scanner;
/**
*
* @author use
*/
public class Excepciones1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
int [] array = {4,2,6,7};
int n;
boolean repetir = false;
do{
try{
repetir = false;
System.out.print("Introduce un nmero entero > 0 y < " + array.length + ": ");
n = sc.nextInt();
System.out.println("Valor en la posicin " + n + ": " + array[n]);
}catch(InputMismatchException e){
sc.nextLine();
n = 0;
System.out.println("Debe introducir un nmero entero ");
repetir = true;
}catch(IndexOutOfBoundsException e){
System.out.println("Debe introducir un nmero entero > 0 y < " + array.length + " ");
repetir = true;
}catch(Exception e){ //resto de excepciones de tipo Exception y derivadas
System.out.println("Error inesperado " + e.toString());
repetir = true;
}
}while(repetir);
}
}

package excepciones2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author use
*/
public class Excepciones2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
// TODO code application logic here
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
int a, b;
try{
System.out.println("Introduce numero 1: ");
a = Integer.parseInt(br.readLine());
System.out.println("Introduce numero 2: ");
b = Integer.parseInt(br.readLine());
System.out.println("La division es: " + (a/b));
}
catch(ArithmeticException e){
System.out.println("ERROR: no se puede dividir entre 0");
}
catch(Exception e){
System.out.println("ERROR");
}
}
}

Você também pode gostar