Slides Input & Output (IO) Working With Files in Java Exception Handling Checked vs Unchecked the Finally Clause
Slides Input & Output (IO) Working With Files in Java Exception Handling Checked vs Unchecked the Finally Clause
LBYL stands for, "Look Before You Leap". This style of coding involves checking for
errors, before you perform an operation.
EAFP stands for, "Easier to Ask Forgiveness than Permission". This assumes an
operation will usually succeed, and then handles any errors that occur, if they do
occur.
Like all questions about software, the answer is, that depends.
Check for errors before performing an Assume that the operation will succeed
Approach
operation. and handle any errors that occur.
Advantages Can be more efficient if errors are rare. Can be more concise and easier to read.
Can be more verbose if errors are Can be more difficult to debug if errors
Disadvantages
common. are unexpected.
When you're coding with an IDE, it's pretty easy to recognize one, because your
code won't compile, if one is thrown from code you're calling.
A checked exception means it's NOT an unchecked exception.
An Unchecked Exception is an instance of a RuntimeException, or one if its
subclasses.
Let me just pull RuntimeException up, in the API documentation.
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/RuntimeException.html
The original purpose for the finally clause, was to have a single block of code to
perform cleanup operations
This included closing open connections, releasing locks, or freeing up resources.
This clause ensured that this code was executed, both during normal completion as
well as in the event of an exception.
The try with resources syntax, introduced in JDK7, is a better approach than
using the finally clause for closing resources.
The finally clause can be used to execute other important tasks, such as logging or
updating the user interface.
• It can be difficult to read and understand code, that uses this clause.
• It can be used to hide errors, which can make debugging more difficult.
• If you execute code that's not related to cleanup tasks, this will make it harder to
maintain your code.