Skip to content

Instantly share code, notes, and snippets.

@ldaniel
Created January 25, 2012 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ldaniel/1677933 to your computer and use it in GitHub Desktop.
Save ldaniel/1677933 to your computer and use it in GitHub Desktop.
Tratando erros no Java
public void openFile(){
try {
FileReader reader = new FileReader("arquivo.txt");
int i=0;
while(i != -1){
i = reader.read();
System.out.println((char) i );
}
reader.close();
System.out.println("-> EOF");
} catch (FileNotFoundException e) {
// acoes em caso de excecao do tipo "file not found"
} catch (IOException e) {
// acoes em caso de excecao de IO
}
finally {
// acoes finais que serao executadas independentemente
// de ter ocorrido uma exception
}
}
// ou ainda
public void doSomething() throws IOException, FileNotFoundException{
// codigo aqui
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment