From f8d612969c8049cb4ce50e5f787f222142832867 Mon Sep 17 00:00:00 2001 From: Thabo Ntsoko Date: Sun, 22 Dec 2019 17:08:39 +0200 Subject: [PATCH] Moving CatchingThrowable.java to the core-java-exceptions-2 module --- .../exceptions/CatchingThrowable.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/exceptions/CatchingThrowable.java diff --git a/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/exceptions/CatchingThrowable.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/exceptions/CatchingThrowable.java new file mode 100644 index 0000000000..20b06cdd26 --- /dev/null +++ b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/exceptions/CatchingThrowable.java @@ -0,0 +1,38 @@ +package com.baeldung.exceptions; + +import java.util.Set; +import java.util.UUID; + +public class CatchingThrowable { + + class CapacityException extends Exception { + CapacityException(String message) { + super(message); + } + } + + class StorageAPI { + + public void addIDsToStorage(int capacity, Set storage) throws CapacityException { + if (capacity < 1) { + throw new CapacityException("Capacity of less than 1 is not allowed"); + } + int count = 0; + while (count < capacity) { + storage.add(UUID.randomUUID().toString()); + count++; + } + } + + // other methods go here ... + } + + public void add(StorageAPI api, int capacity, Set storage) { + try { + api.addIDsToStorage(capacity, storage); + } catch (Throwable throwable) { + // do something here + } + } + +}