Category: Using Escape Sequences in Text Blocks
-
Chaining Exceptions – Exception Handling
Chaining Exceptions It is common that the handling of an exception leads to the throwing of another exception. In fact, the first exception is the cause of the second exception being thrown. Knowing the cause of an exception can be useful, for example, when debugging the application. The Java API provides a mechanism for chaining…
-
Implementing the AutoCloseable Interface – Exception Handling
Implementing the AutoCloseable Interface A declared resource must provide the close() method that will be called when the resource is to be closed. This is guaranteed by the fact that the resource must implement the java.lang.AutoCloseable interface which specifies the close() method. The compiler only allows resource declarations or resource variables in the try header…
-
The try-with-resources Statement 2 – Exception Handling
A lot of boilerplate code is required in explicit resource management using try-catch-finally blocks, and the code can get tedious and complex, especially if there are several resources that are open and they all need to be closed explicitly. The try-with-resources statement takes the drudgery out of associating try blocks with corresponding finally blocks to…
-
Advantages of Exception Handling – Exception Handling
7.8 Advantages of Exception Handling Robustness refers to the ability of a software system to respond to errors during execution. A system should respond to unexpected situations at runtime in a responsible way. Applications that provide the user with frequent cryptic messages with error codes or that repeatedly give the user the silent treatment when…
-
Creating and Initializing Strings – Selected API Classes
Creating and Initializing Strings Immutability The String class implements immutable character strings, which are read-only once the string has been created and initialized. Objects of the String class are thus thread-safe, as the state of a String object cannot be corrupted through concurrent access by multiple threads. Operations on a String object that modify the…
-
Converting Integer Values to Strings in Different Notations – Selected API Classes
Converting Integer Values to Strings in Different Notations The wrapper classes Integer and Long provide static methods for converting integers to text representations in decimal, binary, octal, and hexadecimal notation. Some of these methods from the Integer class are listed here, but analogous methods are also defined in the Long class. Example 8.2 demonstrates the…
-
Converting Primitive Values to Strings – Selected API Classes
Converting Primitive Values to Strings Each wrapper class defines a static method toString(type v) that returns the string corresponding to the primitive value of type, which is passed as an argument ((6a) in Figure 8.2). Click here to view code image static String toString(type v) Click here to view code image String charStr2 = Character.toString(‘\n’); //…
-
Suppressed Exceptions – Exception Handling
Suppressed Exceptions The program output from GizmoTest in Example 7.18 shows that the Arithmetic-Exception was thrown in the compute() method called in the try block at (4) before the IllegalArgumentException was thrown by the implicit call to the close() method. Since only one exception can be propagated, the IllegalArgumentException thrown last would mask the ArithmeticException…
-
Extracting Substrings from Strings – Selected API Classes
Extracting Substrings from Strings The String class provides methods to trim and strip strings, and also extract substrings. boolean isBlank() Returns true if the string is empty or contains only whitespace; otherwise, it returns false. See also the method isEmpty() in the CharSequence interface (p. 444). String strip()String stripLeading()String stripTrailing() Return a string whose value…
-
Converting Strings to Wrapper Objects – Selected API Classes
Converting Strings to Wrapper Objects Each wrapper class (except Character) defines the static method valueOf(String str) that returns the wrapper object corresponding to the primitive value represented by the String object passed as an argument ((2) in Figure 8.2). This method for the numeric wrapper types also throws a NumberFormatException if the String parameter is…