[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
一:当代码中可能会遇到的异常需要用try{ }catch{Exception e}来处理,否则导致程序崩溃
try{
int i = 1/0;
}catch(Exception e){
........
}
二:不要在for循环里面嵌套try catch子句
for (int i = 0; i < array.length; i++) {
try {
.......
} catch (IOException e) {
.......
} catch (Exception e) {
.......
}
}
改成:
try {
for (int i = 0; i < array.length; i++) {
.......
}
} catch (IOException e) {
.......
} catch (Exception e) {
.......
}
三:尽可能的减少try catch子句的嵌套,会影响性能
try {
try {
.......
} catch (IOException e) {
.......
}
} catch (Exception e) {
.......
}
改成
try {
.......
}catch (IOException e) {
.......
} catch (Exception e) {
.......
}
四:同一个try子句中多个catch时,异常处理原则
当一个try块中包含众多语句,可能抛出多种不同的异常,只有通过多个catch块来捕获不同的异常。而如果两个异常存在继承关系,应该把子类异常放在你父类异常的前面来捕获,或者把最小范围异常放在最前面
,范围大的放在后面,因为是按照catch块从上往下匹配,当它匹配某一个catch块的时候,他就直接进入到这个catch块里面去了,后面在再有catch块的话,它不做任何处理,直接跳过去,全部忽略掉。如果有finally的话进入到finally里面继续执行。Exception这个异常的根类一定要刚在最后一个catch里面,如果放在前面或者中间,任何异常都会和Exception匹配的,就会报已捕获到...异常的错误。同时配合log4j使用会对程序的日后维护帮助很大。
try {
.......
}catch (IOException e) {
.......
} catch (Exception e) {
.......
}