>>102449904
Lifted right out of ChatGPT, more or less.
With checked exceptions, you don't have to figure out what the call can throw, and you don't need to catch everything everywhere either.
public class NetworkService {
public DataObject fetchData(UUID dataId) throws BusinessException {
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
//connection query parameters and whatever else
// Check the response code
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
return new DataObject(); // Assume you did XML or JSON or whatever the fuck parsing and constructed something useful.
} else {
throw new BusinessDataException("Failed to fetch data: Response code " + responseCode);
}
} catch (IOException | XMLException | JSONException e) {
// Everything from NXDOMAIN through timeout errors are children of IOException.
throw new BusinessDataException("Could not get data from data service.", e);
} catch (Exception e) {
// Handle other checked exceptions as needed
throw new BusinessDataException("Something fucky happened getting data, and we didn't think we would get this exception.", e);
}
}
}
public class DoShitService {
public void doShit(UUID dataId) {
try {
DataObject data = networkService.fetchData(dataId);
// do shit with data
catch (BusinessException e) {
LoggerPackage.logShit("We couldn't do shit.", e);
// If not doing shit is crash worthy, either throw a new RuntimeException or start calling System.exit() and whatever. Otherwise, do nothing.
}
}