try catch java exception code example
Example 1: java try catch
try {
// Code that may have error
} catch(ErrorName e){
// Another code
}
Example 2: java try catch
try {
// Code to try, which is throwing an Exception, e.g.
/*example*/ Thread.sleep(100)
} catch (InterruptedException e /*Or any other exception*/) {
// Handle Exception, usually:
e.printStackTrace(); // Print the StackTrace of the exception to see what cause it
} finally {
// Code executed after try / catch, used to close streams
/*example*/ in.close();
}