Not sure if you're referring directly to RuntimeException
in Java, so I'll assume you're talking about run-time exceptions.
The basic idea of exception handling in Java is that you encapsulate the code you expect might raise an exception in a special statement, like below.
try {
// Do something here
}
Then, you handle the exception.
catch (Exception e) {
// Do something to gracefully fail
}
If you need certain things to execute regardless of whether an exception is raised, add finally
.
finally {
// Clean up operation
}
All together it looks like this.
try {
// Do something here
}
catch (AnotherException ex) {
}
catch (Exception e) { //Exception class should be at the end of catch hierarchy.
}
finally {
}