java异常处理的方式有两种
1.捕捉异常
2.转移异常
这里先说捕捉异常(对1个系统而言,异常处理是非常关键的,处理得好能够避免和减少系统bug,同时也能够非常好的定位问题来源)
捕捉异常都是通过try-catch-finally结构处理,1般有两种格式
以下所示:
try {
//可能会产生异常的语句
} catch (Exception e) {
//捕捉到异常后的处理语句
}
//结构2
try {
//可能会产生异常的语句
} catch (Exception e) {
//捕捉到异常后的处理语句
}
finally {
}
注意:catch和finally不1定同时存在。但是try必须与二者之1共同存在,也就是说结构可以是try-catch和try-finally
写1个简单的异常捕捉例子
public static void main(String[] args) {
try {
//被除数不能为0,程序抛出异常
int i = 0, j = 1;
int k = j / i;
} catch (Exception e) {
System.out.println("已捕捉到异常");
e.printStackTrace();
}
}
}
如上i分母不能为0,所以try程序中的语句就会抛出异常,然后catch捕捉到异常并履行catch中的语句
运行结果以下:
java.lang.ArithmeticException: / by zero
at ExceptionTest.main(ExceptionTest.java:7)
那到底甚么时候catch会履行甚么时候不会履行呢?和finally甚么时候会履行?
其实catch中的语句如果在捕捉到异常的情况下都会履行,当try语句块中抛出异常,该异常实力对象就会顺次匹配在各个catch语句块中的类型Exception1、Exception2、Exception3…..。1旦匹配上就会进入相应的catch语句块并履行。此时则为异常被catch语句块捕捉到
而finally语句块1般总是会被履行,不管是不是产生了异常程序都会履行。如果履行到try或是catch语句块中的return语句,则先运行finally语句块
以下:
public static void main(String[] args) {
try {
// 抛出(throw)异常
System.out.println("履行try语句块,抛出异常");
throw new Error();
} catch (Exception e) {
System.out.println("履行catch语句块,已捕捉到Exception异常");
e.printStackTrace();
} catch (Error e) {
System.out.println("履行catch语句块,已捕捉到Error毛病");
e.printStackTrace();
} finally {
System.out.println("履行finally语句块");
}
}
}
运行结果:
java.lang.Error
at ExceptionTest.main(ExceptionTest.java:8)
履行catch语句块,已捕捉到Error毛病
履行finally语句块
固然当try中不做任何操作,finally也会履行。
波比源码 » try-catch-finally异常处理(一)
levaquin pills levaquin order online
accutane 20mg canada purchase amoxicillin generic buy zithromax 250mg
buspar 5mg usa order dilantin 100 mg without prescription cost oxybutynin
buy nootropil 800 mg online viagra order online sildenafil 100mg pills for sale
buy lopressor online cheap purchase metoprolol online cheap vardenafil over the counter
online blackjack for real money usa casino slots online casino bonus
order generic deltasone isotretinoin 40mg pills amoxicillin 500mg oral
slot machines global poker online order cialis 5mg online
generic azithromycin 250mg buy prednisolone 10mg online cheap neurontin pills
tadacip 10mg generic tadalafil 10mg cost indomethacin 75mg usa
oral naproxen 250mg order naproxen pill lansoprazole sale
brand clarithromycin 250mg catapres 0.1 mg for sale cost meclizine
buy proventil 100 mcg albuterol 100 mcg over the counter buy cipro 500mg for sale
ivermectin uk buy symmetrel 100mg without prescription buy avlosulfon online cheap
adalat 10mg oral order perindopril 8mg online allegra 120mg pills
buy altace 5mg sale buy generic arcoxia 120mg order arcoxia 60mg pill
order olmesartan generic generic olmesartan 20mg buy depakote 500mg sale
clobetasol online buy temovate online buy amiodarone online cheap
order diamox 250 mg online order generic imdur 40mg oral imuran 25mg
order generic alendronate 35mg furadantin 100mg tablet buy motrin 400mg for sale
buy generic priligy 90mg priligy 30mg tablet oral domperidone
pamelor over the counter purchase paracetamol sale paxil 10mg us
generic famotidine pepcid 40mg brand purchase mirtazapine online cheap
purchase tricor pills buy tricor 200mg generic viagra 100mg england
Thank you for great information. Hello Administ . Metropol Halı Karaca Halı Öztekin ve Selçuklu Halı Cami Halısı ve Cami Halıları Türkiye’nin En Büyük Cami Halısı Fabrikasıyız…
buy cialis 40mg pill Fda approved cialis online ed medications
glucophage 500mg brand generic metformin order nolvadex 10mg pill
provigil 100mg canada provigil 200mg canada phenergan price
oral prednisone 40mg order accutane 20mg online amoxil 250mg generic
ondansetron 8mg generic trimethoprim ca bactrim 960mg oral
buy ventolin inhalator generic augmentin 375mg cost augmentin without prescription
order provigil 200mg generic lopressor for sale order lopressor 100mg without prescription
buy generic prednisolone 40mg buy gabapentin 100mg online lasix 100mg cheap
dutasteride sale buy orlistat online cheap purchase xenical online cheap
azathioprine 25mg us micardis cheap naproxen 500mg over the counter
buy cefdinir 300 mg generic prevacid 15mg brand order protonix 20mg generic