Created
October 28, 2019 06:57
-
-
Save dfparker2002/7b9e0e6a3bdc741f9a05879c0cc9df62 to your computer and use it in GitHub Desktop.
Catching Throwable demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src: https://github.com/eugenp/tutorials/blob/f9259fce764200d1e12d7b970d01feb8d9267edb/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/catchingthrowable/ThrowableClassDemo.java | |
import java.util.HashSet; | |
import java.util.Set; | |
import java.util.UUID; | |
public class ThrowableClassDemo { | |
class CapacityException extends Exception { | |
CapacityException(String message) { | |
super(message); | |
} | |
} | |
public void addIDsToStorage(int capacity, Set<String> storage) throws CapacityException { | |
if (capacity < 0) { | |
throw new CapacityException("Negative capacity not allowed"); | |
} | |
int count = 0; | |
boolean capacityNotReached = true; | |
while (capacityNotReached) { | |
storage.add(UUID.randomUUID().toString()); | |
count++; | |
capacityNotReached = count < capacity; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment