Skip to content

Commit dca7d73

Browse files
author
Thabo Ntsoko
committed
Moving CatchingThrowable.java to the core-java-exceptions-2 module
1 parent 35bc3d6 commit dca7d73

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.exceptions;
2+
3+
import java.util.Set;
4+
import java.util.UUID;
5+
6+
public class CatchingThrowable {
7+
8+
class CapacityException extends Exception {
9+
CapacityException(String message) {
10+
super(message);
11+
}
12+
}
13+
14+
class StorageAPI {
15+
16+
public void addIDsToStorage(int capacity, Set<String> storage) throws CapacityException {
17+
if (capacity < 1) {
18+
throw new CapacityException("Capacity of less than 1 is not allowed");
19+
}
20+
int count = 0;
21+
while (count < capacity) {
22+
storage.add(UUID.randomUUID().toString());
23+
count++;
24+
}
25+
}
26+
27+
// other methods go here ...
28+
}
29+
30+
public void add(StorageAPI api, int capacity, Set<String> storage) {
31+
try {
32+
api.addIDsToStorage(capacity, storage);
33+
} catch (Throwable throwable) {
34+
// do something here
35+
}
36+
}
37+
38+
}

0 commit comments

Comments
 (0)