Java 17 Recipes


-4. Catching Multiple Exceptions


Download 3.2 Mb.
Pdf ko'rish
bet172/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   168   169   170   171   172   173   174   175   ...   245
Bog'liq
Java 17 Recipes

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:
1   ...   168   169   170   171   172   173   174   175   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling