Tries to execute the tryStatment, and executes the appropriate catch statement if there is an exception.
Syntax
try {
tryStatement
}
[catch (exceptionA if conditionA) {
catchStatementA
}]
...
[catch (exceptionB) {
catchStatementB
}]
[finally {
finallyStatement
}]
Example
openMyFile()
try {
// tie up a resource
writeMyFile(theData);
}
finally {
closeMyFile(); // always close the resource
}
Remarks
Use try...catch as a way to catch exceptions.