public void foo(final Bar bar) {
Objects.requireNonNull(bar);
// use bar
}
Configuration(String source, String destination,
int height, int width) {
new Preconditions()
.notNull(destination)
.notNull(source)
.isPositive(height)
.isPositive(width)
.throwIfErrorsArePresent();
this.source = Paths.get(source);
this.destination = Paths.get(destination);
this.height = height;
this.width = width;
}
public class Preconditions {
private List<String> messages = new ArrayList<>();
public Preconditions notNull(Object value) {
if (value == null) {
this.messages.add("Not present");
}
return this;
}
public Preconditions isPositive(int value) {
if (value < 0) {
this.messages.add("Not positive");
}
return this;
}
public void throwIfErrorsArePresent() {
if (this.messages.size() > 0) {
throw new BadConfigurationFileException(this.messages);
}
}
}
public InsufficientFunds extends Exception {
private double amount;
public InsufficientFunds(double amount) {
this.amount = amount;
}
public double getAmount() {
return amount;
}
}
void charge(CreditCard card, double amount)
throws InsufficientFunds {
boolean canCharge = validate(card, amount);
if (!canCharge) {
throw new InsufficientFunds(amount);
}
// continue transaction
}
void foo() {
try {
// code at a higher layer
} catch (InsufficientFunds e) {
view.addObject("error",
"Couldn't charge " + e.getAmount());
}
}
public void charge() throws SQLException {
// go to database and try to charge
}
public void foo() {
try {
// code at a higher layer
} catch (SQLException e) {
// handle it!
// what happens if I refactor
// the low-level code and a
// new kind of exception is thrown?
}
}
public void charge()
throws PersistenceException {
try {
// code
} catch (SQLException e) {
throw PersistenceException(e);
}
}
public void foo()
throws MyAppException {
try {
// code
} catch (IOException e) {
throws MyAppException("Bad, I'm losing the previous stack trace");
}
}
public void foo()
throws MyAppException {
try {
// code
} catch (IOException e) {
throws MyAppException("Much better!", e);
}
}
public void bar() {
try {
foo(); // throws IOException
baz(); // throws IOException
} catch (IOException e) {
// how do I handle foo's
// and bar's error differently?
}
}
public void foo()
throws MyAppException {
try {
// code
} catch (IOException e) {
throws MyAppException(e);
}
}
public void bar() {
try {
foo();
baz();
} catch (IOException e) {
// code to handle IOE
} catch (MyAppException e) {
// code to handle MAE
}
}
public void foo() {
try {
// code
} catch (FileNotFound e) {
log.warn(e);
} catch (XMLParse e) {
log.warn(e);
}
}
public void foo() {
try {
// code
} catch (FileNotFound | XMLParse e) {
log.warn(e);
}
}
public void foo() {
try {
} catch () {
}
// code
try {
} catch () {
}
}
public void foo() {
try {
// code
try {
} catch () {
}
// code
} catch () {
}
}
public void foo() {
try {
} catch () {
// code
try {
} catch () {
}
// code
}
}
public void foo() {
try {
// code
} catch (MyAppException e) {
// handling of MAE
}
}
public Foo xyz() {
Bar bar = getBar();
Foo foo = null;
try {
foo = bar.getFoo();
} catch (BooException e) {
foo = new Foo();
}
return foo;
}
public Foo xyz() {
try {
Bar bar = getBar();
Foo foo = bar.getFoo();
return foo;
} catch (BooException e) {
return new Foo();
}
}
public void foo() {
Bar bar = getBar();
Foo foo = bar.getFoo();
doSomething(foo);
}
* * *
public void doSomething(Foo foo) {
try {
// use foo
} catch (BooException e) {
log.warn("This foo " + foo);
// code
}
}
private doProcessImage(Path filepath)
throws FileNotFoundException, IOException {
Image image = ImageIO.read(filepath.toFile());
if (image.isProportional(this.height, this.width)) {
image = image.resize(this.height, this.width);
image.write(this.destinationFolder);
}
}
public processImage(Path filepath) {
try {
doProcessImage(filepath);
} catch (FileNotFoundException e) {
System.out.println("Path doesn't exists");
} catch (IOException e) {
System.out.println("File could not be read");
}
}
"... all exceptions thrown by Hibernate
were checked exceptions [...] it soon
became clear that this doesn’t make
sense, because all exceptions thrown
by Hibernate are fatal. In many cases,
the best a developer can do in this situation is to clean up, display an error message, and exit the application. Therefore, starting with Hibernate 3.x, all exceptions thrown by Hibernate are subtypes of the unchecked Runtime Exception, which is usually handled in a single location in an application."
-- Christian Bauer in Hibernate in Action
public int doubleIt(final int n) {
return n * 2;
}
Since, for example, doubleIt(1) will always return 2, then you can safely change call of doubleIt(1) with 2 and your program will behave the same
private ImageResizer(final Builder builder) throws IOException {
FilesUtil.ensureDirectoryExists(builder.destination);
this.destination = builder.destination;
this.source = builder.source;
this.width = builder.width;
this.height = builder.height;
}
public static void run(final String path) {
try {
Properties properties = new Properties();
properties.load(new FileInputStream(path));
Configuration config = new Configuration(properties);
ImageResizer resizer = new ImageResizer(config);
resizer.run();
} catch (IOException e) {
System.err.println("Error :(");
}
}
public Try<ImageResizer>(final Builder builder) throws IOException {
return Try.run(() -> FilesUtil.ensureDirectoryExists(builder.destination))
.map(v -> new ImageResizer(builder));
}
public static void run(final String path) {
Try.of(Properties::new)
.flatMap(p -> Try.of(properties.load(path)))
.flatMap(prop -> Try.of(Configuration::new))
.map(config -> new ImageResizer(config))
.run(ImageResizer::run)
.onFailure(ex -> System.err.println(":(");
}
public static Either<IOException, Configuration> newInstance(final String path) {
try {
Properties prop = new Properties();
prop.load(new FileInputStream(path));
return Either.right(new Configuration(prop));
} catch (IOException e) {
return Either.left(e);
}
}
public static void run(final String path) {
Configuration.newInstance(path)
.map(ImageResizer::new)
.flatMap(ImageResizer::run)
.orElseRun(ex -> System.err.println("Error :("));
}