Tipos de Dados e Exceções em Java
Jim White
Java Developer
public static void main(String[] args) {
ArrayList<String> allStars = new ArrayList<String>();
allStars.add("Mays");
allStars.add("Aaron");
allStars.add("Ruth");
String last = allStars.get(3); // This will cause an exception
System.out.println(last);
}
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 3 out of
bounds for length 3
try {
// Instruções a executar e monitorar por exceções
}
try {
// Instruções a executar e monitorar por exceções
} catch (<Exception type> <variable name>) {
// Instruções a executar se ocorrer uma exceção
}
try {
ArrayList<String> allStars = new ArrayList<String>();
allStars.add("Mays");
allStars.add("Aaron");
allStars.add("Ruth");
String last = allStars.get(3); // Accessing an element that isn't there
System.out.println(last);
} catch (IndexOutOfBoundsException e) {
System.out.println("Ops - índice incorreto");
}
Ops - índice incorreto
catch para tratar tipos diferentes de exceçõescatch tem seu próprio tipo e variável de exceçãocatch em ordem até achar um correspondentetry {
ArrayList<String> nums
= new ArrayList<String>();
nums.add("one");
String last = nums.get(0);
Integer.valueOf(last);
System.out.println(last);
} catch (IndexOutOfBoundsException eIndex) {
System.out.println("Ops - índice incorreto");
} catch (NumberFormatException eValueOf) {
System.out.println(
"Ops - String não é número");
}
Ops - String não é número
catch (Exception variable){ } como um captura-tudotry {
// Código a tentar
} catch (IndexOutOfBoundsException eIndex) {
System.out.println("Ops - índice incorreto");
} catch (NumberFormatException eValueOf) {
System.out.println("Ops - String não é número");
} catch (Exception e) { // Trata todas as exceções exceto as duas acima
System.out.println("Aconteceu algo não previsto");
}
finally sempre é executado// Quando ocorre exceção
try {
Integer.valueOf("one");
} catch (NumberFormatException eValueOf) {
System.out.println("Ops");
} finally {
System.out.println("Fazendo limpeza");
}
Ops
Fazendo limpeza
// Quando nenhuma exceção é lançada
try {
Integer.valueOf("1");
} catch (NumberFormatException eValueOf) {
System.out.println("Ops");
} finally {
System.out.println("Fazendo limpeza");
}
Fazendo limpeza
Exception passado ao catch traz muitas informações.getMessage() para obter a causa legível.getClass() para obter a classe/tipo da exceção .printStackTrace() para exibir o stack trace (próximo slide)public class ExceptionExample {
public static void main(String[] args) {
try {
Integer.valueOf("one");
} catch (NumberFormatException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
For input string: "one"
java.lang.NumberFormatException: For input string: "one"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:661)
at java.base/java.lang.Integer.valueOf(Integer.java:988)
at foo/exceptions.ExceptionExample.main(ExceptionExample.java:7)
Tipos de Dados e Exceções em Java