Java 17 Recipes
-4. Catching Multiple Exceptions
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
- Bu sahifa navigatsiya:
- How It Works
9-4. Catching Multiple Exceptions
Problem A block of code in your application has the possibility of throwing multiple exceptions. You want to catch each of the exceptions that may occur within a try block. Solution 1 More than one catch clause can be specified when multiple exceptions may be encountered within the same block. Each catch clause can specify a different exception to handle so that each exception can be handled differently. In the following code, two catch clauses handle IOException and a ClassNotFoundException. public static void main(String[] args) { Recipe9_4 recipe = new Recipe9_4(); recipe.startClassic(); } private void startClassic() { try { Class> stringClass = Class.forName("java.lang.String"); FileInputStream in = new FileInputStream("myFile.log") ; // Can throw IOException in.read(); } catch (IOException e) { System.err.println("There was an exception "+e); } catch (ClassNotFoundException e) { System.err.println("There was an exception "+e); } } Chapter 9 exCeptions and Logging 337 Solution 2 If your application tends to throw multiple exceptions within a single block, then a vertical bar operator (|) can be utilized for handling each of the exceptions in the same manner. In the following example, the catch clause specifies multiple exception types separated with a vertical bar (|) to handle each exception in the same manner. public static void main(String[] args) { Recipe9_4 recipe = new Recipe9_4(); recipe.start(); } private void start() { try { Class> stringClass = Class.forName("java.lang.String"); FileInputStream in = new FileInputStream("myFile.log") ; // Can throw IOException in.read(); } catch (IOException | ClassNotFoundException e) { System.out.println("An exception of type "+e.getClass()+" was thrown! "+e); } } How It Works There are many different ways to handle situations where multiple exceptions may be thrown. You can specify separate catch clauses to handle each of the exceptions differently. To handle each exception in the same manner, you can utilize a single catch clause and specify each exception separated with a vertical bar operator. Chapter 9 exCeptions and Logging 338 Note if you’re catching an exception in multiple catch blocks (solution 1), make sure that the catch blocks are defined from the most specific to the most general. Failure to follow this convention prevents an exception from handling the more specific blocks. this is most important when there are catch (Exception e) blocks, which catch almost all exceptions. having a catch (Exception e) block—called a catch-all or Pokémon exception handler (gotta catch them all)—is usually poor practice because such a block catches every exception type and treats them all the same. this becomes a problem because the block can catch other exceptions that may occur deeper within the call stack that you may not have intended the block to catch (OutOfMemoryException). it is best to specify each possible exception rather than specifying a catch-all exception handler to catch all exceptions. Download 3.2 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling