Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet169/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   165   166   167   168   169   170   171   172   ...   245
Bog'liq
Java 17 Recipes

9-1. Catching Exceptions
 Problem
You want to gracefully handle any exceptions generated from your code.
Chapter 9 exCeptions and Logging


331
 Solution
Use the built-in try/catch language construct to catch exceptions. Do so by wrapping 
any blocks of code that may throw an exception within a try/catch block. The 
following example generates a Boolean value to indicate whether a specified string 
is greater than five characters long. If the string passed as an argument is null, a 
NullPointerException (RuntimeException type) is thrown by the length() method and 
caught within the catch block.
public class Recipe9_1 {
public static void main(String[] args) {
Recipe9_1 recipe = new Recipe9_1();
recipe.start();
}
private void start() {
System.out.println("Is th string 1234 longer than 5 chars?:"+
isStringShorterThanFiveCharacters("1234"));
System.out.println("Is th string 12345 longer than 5 chars?:"+
isStringShorterThanFiveCharacters("12345"));
System.out.println("Is th string 123456 longer than 5 chars?:"+
isStringShorterThanFiveCharacters("123456"));
System.out.println("Is th string null longer than 5 chars?:"+
isStringShorterThanFiveCharacters(null));
}
private boolean isStringShorterThanFiveCharacters(String aString) {
try {
return aString.length() > 5;
} catch (NullPointerException e) {
System.err.println("An Exception Occurred: " + e);
return false;
}
}
}
Chapter 9 exCeptions and Logging


332
 How It Works
The try keyword specifies that the enclosed code segment can raise an exception. The 
catch clause is placed at the end of the try clause. Each catch clause specifies which 
exception is being caught. The compiler generates an error if a catch clause is not 
provided for a checked exception. Two possible solutions are to add a catch clause or 
to include the exception in the throws clause of the enclosing method. Any checked 
exceptions that are thrown but not caught propagate up the call stack. If this method 
doesn’t catch the exception, the thread that executed the code terminates. If the thread 
terminating is the only thread in the program, it terminates its execution.
If a try clause needs to catch more than one exception, more than one exception can 
be specified, separated by a bar character. For instance, the following try/catch block 
could be used for catching both NumberFormatException (a RuntimeException type) and 
NullPointerException.
try {
// code here
} catch (NumberFormatException|NullPointerException ex) {
// logging
}
Note Be careful when throwing an exception. if the thrown exception is not 
caught, it propagates up the call stack; and if there isn’t any catch clause capable 
of handling the exception, it causes the running thread to terminate (also known 
as unraveling). if your program has only one main thread, an uncaught exception 
terminates your program.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   165   166   167   168   169   170   171   172   ...   245




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