Skip to content

Instantly share code, notes, and snippets.

@antoc0d3
Created April 25, 2017 22:22
Show Gist options
  • Save antoc0d3/5888a8c3e0f79790babb68d57d13fea2 to your computer and use it in GitHub Desktop.
Save antoc0d3/5888a8c3e0f79790babb68d57d13fea2 to your computer and use it in GitHub Desktop.
Excepciones en JAVA
public static void main(String[] args) {
//Division entre cero
try {
int x = 5 / 0;
} catch (ArithmeticException e) {
System.out.println(e);
}
//Fuera del tamaño del arreglo
try {
int [] y = new int[10];
y[100]= 15;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e);
}
//Fuera del tamaño del String
try {
String cad ="java";
cad.charAt(50);
} catch (StringIndexOutOfBoundsException e) {
System.out.println(e);
}
//Error de puntero null
try {
JButton x = null;
x.setBounds(0,0,100,15);
} catch (NullPointerException e) {
System.out.println(e);
}
//Error de formato
try {
Integer o = Integer.parseInt("jaja");
} catch (NumberFormatException e) {
System.out.println(e);
}
//Error de casteo
try {
//Polimorfismo
Object obj = new JButton();
Integer a = (Integer)obj;
} catch (ClassCastException e) {
System.out.println(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment