Notes on "Elegant Objects (Volume 1)" - continued

Ending Chapter - 4!

Throw Only Checked Exceptions

Unchecked Exceptions are a mistake and all Exceptions must be checked. It is an obvious choice we have to make when designing a method - to catch all exception here and now, making the method look "safe" for its users, or escalate problems. Right choice is b) escalate! Every catch statement has to have a very strong reason for its existence. Do not catch unless you really have to. Always chain exceptions and never ignore original ones. Recover once only at the highest level!
Here is an example..
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
 
public class Main {
 
    public static void main(String[] args) throws Exception {
        final FileOpener notExistingFileOpener = new FileOpener("notExistingFile");
        final FileSizeCalculator fileSizeCalculator = new FileSizeCalculator(notExistingFileOpener);
        try {
            fileSizeCalculator.calculateSize();
        } catch (FileNotFoundException fileNotFoundException) {
            throw new Exception("Could not read size!", fileNotFoundException);
        }
    }
}
 
class FileOpener {
 
    private final String filename;
 
    public FileOpener(String filename) {
        this.filename = filename;
    }
 
    public File openNewFile() {
        final File file = new File(filename);
        return file;
    }
}
 
class FileSizeCalculator {
 
    private final FileOpener fileOpener;
 
    public FileSizeCalculator(FileOpener fileOpener) {
        this.fileOpener = fileOpener;
    }
 
    public long calculateSize() throws FileNotFoundException {
        final File file = fileOpener.openNewFile();
        final FileInputStream fileInputStream = new FileInputStream(file);
        return file.length();
    }
}
And when executed..
Korays-MacBook-Pro:~ koraytugay$ javac Main.java 
Korays-MacBook-Pro:~ koraytugay$ java Main
Exception in thread "main" java.lang.Exception: Could not read size!
 at Main.main(Main.java:13)
Caused by: java.io.FileNotFoundException: notExistingFile (No such file or directory)
 at java.io.FileInputStream.open0(Native Method)
 at java.io.FileInputStream.open(FileInputStream.java:195)
 at java.io.FileInputStream.<init>(FileInputStream.java:138)
 at FileSizeCalculator.calculateSize(Main.java:42)
 at Main.main(Main.java:11)
Yes, I like this! If I recover at this point, I will have a nice stacktrace! And this is the point to recover, the highest level.

Use Aspect - Oriented Programming

NOT!

Be Either Final or Abstract

I really like this chapter and this suggestion, be either 'final' or 'abstract'..
The strongest argument agains inheritance is that it makes relations between objects too complex. It is simply very difficult to understand a hierarchy of classes that inherit each other when the height of such a pyramid is five levels or more.. However, there is a solution: make your classes either final or abstract. We have to be explicit in our intentions - either a method is designed the right way or it is not designed at all. There is nothing in between!
Again, wise words from a wise man