Table of Contents
I wrote the summary about try
, catch
and finally
statement in Java.
Overview and simple format
try .. catch statement can capture exceptions thrown in the program. Simple try .. catch statement must be written in the following form. In that form, when an exception is thrown in the statements 1, then the program goes to catch block and check whether the type of the error is ErrorOrExceptionType
. If yes, then statement 2 is executed, otherwise the program is interrupted and stopped. In statements 2, the error information can be used with the variable ex, which was defined in the parenthesis just after catch.
Format
1 2 3 4 5 |
try { /* statements 1 */ } catch (ErrorOrExceptionType ex) { /* statements 2 */ } |
Example
Here is an example. This example reads from characters from standard input as int and divide 100 by it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
try { System.out.print("Input number: "); java.util.Scanner scanner = new java.util.Scanner(System.in); int i = scanner.nextInt(); int j = 100 / i; System.out.println("result: " + j); } catch (ArithmeticException e) { System.err.println("ArithmeticException was caught"); System.err.println(e.getMessage()); } // Output 1: // Input number: 1 <- 1 is input // result: 100 // Output 2: // Input number: 0 <- 0 is input // ArithmeticException was caught // / by zero |
Especially, when we write catch (Exception e)
, we can handle any exception and we don’t have to check all the condition such as the file existence and permission when we use files for example. But catching Exception
is not always effective, because in the product, we have to pay attention to what kind of message must be sent to users according to the error or exception types. In those cases, multiple catch blocks make sense.
Multiple catch blocks
try .. catch statement can include multiple catch blocks as follows. In that form, the exception is compared to ErrorOrExceptionType1
first, and if they are not the same, the error is compared to the following ErrorOrExceptionTypes
, step by step from up to down. When the exception type is the same as one ErrorOrExceptionType
, the statements in its catch block are executed. Once the exception is caught, its following catch block is not executed.
Format
1 2 3 4 5 6 7 |
try { /* statements 1 */ } catch (ErrorOrExceptionType1 ex) { /* statements 2 */ } catch (ErrorOrExceptionType2 ex) { /* statements 3 */ } |
Example
This example reads from characters from standard input, converts it to int and divide 100 by it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
try { System.out.print("Input number: "); StringBuilder s = new StringBuilder(); while (true) { char c = (char) System.in.read(); if (c == 'n') break; s.append(c); } int i = Integer.parseInt(s.toString()); int j = 100 / i; System.out.println("result: " + j); } catch (IOException e) { System.err.println("IOException was caught"); System.err.println(e.getMessage()); } catch (NumberFormatException e) { System.err.println("NumberFormatException was caught"); System.err.println(e.getMessage()); } catch (ArithmeticException e) { System.err.println("ArithmeticException was caught"); System.err.println(e.getMessage()); } // Output 1: // Input number: 1 <- 1 is input // result: 100 // Output 2: // Input number: <- Enter without any input // NumberFormatException was caught // For input string: "" // Output 3: // Input number: 0 <- 0 is input // ArithmeticException was caught // / by zero |
When we use a database, sometimes SQLException
is thrown according to the database status. SQLException
class has some methods which are not in Exception
class, such as getSQLState
, getErrorCode
. Catching SQLException
is meaningful to know more information about the database and its connection. So catching SQLException
at first is meaningful.
1 2 3 4 5 6 7 8 9 |
try { // ... } catch (SQLException e) { System.err.println(e.getSQLState()); System.err.println(e.getErrorCode()); // ... } catch (Exception e) { // ... } |
Finally block
A finally block can be added after catch block as follows. finally block is executed after try and catch block regardless of error existence.
Format
1 2 3 4 5 6 7 |
try { /* statements 1 */ } catch (ErrorOrExceptionType ex) { /* statements 2 */ } finally { /* statements 3 */ } |
Example
In this example, “finished.” is always printed out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
try { System.out.print("Input number: "); java.util.Scanner scanner = new java.util.Scanner(System.in); int i = scanner.nextInt(); int j = 100 / i; System.out.println("result: " + j); } catch (ArithmeticException e) { System.err.println("ArithmeticException was caught"); System.err.println(e.getMessage()); } finally { System.out.println("finished."); } // Output 1: // Input number: 1 <- 1 is input // result: 100 // finished. // Output 2: // Input number: 0 <- 0 is input // ArithmeticException was caught // / by zero // finished. |