The try Statement The try statement is used wherever there is code that could throw an exception. It involves the use of three reserved words: try, catch and finally. The syntax is the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
try { // code that could throw an exception of any of the types below } catch (ExceptionType1 e) { // exception handler code here } catch (ExceptionType2 e) { // exception handler code here } … catch (ExceptionTypeN e) { // exception handler code here } finally { // cleaning and closing resources here } |
The try statement must contain at least one catch block or one finally block. The ..Continue Reading