You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

54 lines
1.8 KiB

import java.util.concurrent.atomic.AtomicInteger;
public class ExceptionDemo {
public static void main(String[] args) {
System.out.println("===== 测试 URL 校验测试 =====");
testUrlValidation();
System.out.println("\n===== 测试重试机制测试 =====");
testRetryMechanism();
}
private static void testUrlValidation() {
String[] testUrls = {
"https://www.example.com",
"invalid-url",
null
};
for (String url : testUrls) {
try {
System.out.println("正在校验 URL: " + url);
UrlValidator.validateUrl(url);
System.out.println("URL 格式有效!");
} catch (UrlFormatException e) {
System.err.println("错误: " + e.getMessage());
System.err.println("无效的 URL: " + e.getInvalidUrl());
}
}
}
private static void testRetryMechanism() {
// 模拟一个会失败几次的操作
AtomicInteger attemptCounter = new AtomicInteger(0);
try {
String result = RetryUtils.executeWithRetry(() -> {
int currentAttempt = attemptCounter.incrementAndGet();
System.out.println("执行操作... (尝试次数: " + currentAttempt + ")");
// 模拟前3次失败,第4次成功
if (currentAttempt < 4) {
throw new RuntimeException("模拟失败");
}
return "操作成功完成!";
});
System.out.println("结果: " + result);
} catch (NetworkException e) {
System.err.println("错误: " + e.getMessage());
e.printStackTrace();
}
}
}