CH11 - Exceptions and Localization Flashcards
(17 cards)
Unchecked Exception
An unchecked exception is any exception that does not need to be declared or handled by the application code it is thrown.
Unchecked exceptions are often referred to as runtime exceptions.
Error
Error means something went so horribly wrong that your program should not attempt to recover from it.
Throwable
Throwable is that it’s the parent class of all exceptions, including the Error class.
While you can handle Throwable and Error exceptions, it is not recommended you do so in your app code.
Checked Exception
Subclass of Exception but not subclass of RuntimeException
Unchecked Exception
Subclass of RuntimeException
Overriding method with Exception
An overridden method may not declare any new or broader checked exceptions that the method it inherits.
An overridden method in a subclass is allowed to declare fewer exceptions than the superclass or interface.
try-with-resources
Only classes that implement the AutoCloseable interface can be used in a try-with-resources statement.
Inheriting AutoClosable requires implementing a compatible close() method
Closable
Closeable extends AutoClosable they are both supported in try-with-resources statements.
Difference Closeable’s close() method declares IOException, while AutoCloseable’s close() method declares Exception.
Scope of Try-With-Resources
The resources created in the try clause are in scope only within the try block.
This is anoter way to remember that the implicit finally runs before any catch/finally blocks that you code yourself.
📦 NumberFormat — What It Does
-Formats numbers as plain numbers, currency, or percentages
-Parses strings back into numbers
-Adapts to locales (e.g., decimal separator in US vs Germany)
Common Subclasses of NumberFormat
NumberFormat
DecimalFormat
Currency
Percent
Basic Number Formatting
NumberFormat nf = NumberFormat.getInstance();
System.out.println(nf.format(1234567.89)); // e.g., “1,234,567.89” (in US)
Locale-Specific Formatting
NumberFormat nfFR = NumberFormat.getInstance(Locale.FRANCE);
System.out.println(nfFR.format(1234567.89));
// e.g., “1234567,89”
📌 Rules for Custom Text:
Wrap any literal text in single quotes ‘…’.
To write a single quote character, use ‘’ (double single quote).
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“hh:mm a ‘o’‘clock’”);
// Output: 03:25 PM o’clock
Unchecked exceptions can be thrown in any method,
Is it coorect claim?
YES
In Java, unchecked exceptions (also called runtime exceptions) are:
Subclasses of RuntimeException
Not required to be declared in a method’s throws clause
Can be thrown in any method, at any time
Multi-catch block
In multi-catch block, all caught exception types must be disjoint - i.e. no subtype - supertype relationships.
Otherwise, one of the exception types would never be reached.
In a multi-catch block, all exception types must be unrelated(disjoint) - no subtype/supertype relationship - to avoid unreachable code.
try-with-resources
It must be either:
-Declared inside the try block
- Effectively final (nor reassigned after initialized)