try,catch,finally,throw,throws学习

1、try,catch,finally总结:

程序1:

package com.trycatch.test6;

public class TryCatchTest6 {
public static void main(String[] args) {
int a[] = { 1 , 2 , 3 };
try {
// int b=5/0;
for ( int i = 0 ; i < a.length; i ++ ) {
System.out.println(
" a== " + a[i]);
}
int b = 5 / 0 ;
}
catch (ArithmeticException e) {
System.out.println(
" ArithmeticException...b== " );
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println(
" ArrayIndexOutOfBoundsException... " );
}
catch (Exception e) {
System.out.println(
" error,but i did it " );
}
finally {
System.out.println(
" mainfinally... " );
}
int c = method1();
System.out.println(
" end...c== " + c);
}

public static int method1() {
try {
int d = 5 / 0 ;
}
catch (ArithmeticException e) {
System.out.println(
" ArithmeticException...d== " );
return 3 ;
}
catch (Exception e) {
return 4 ;
}
finally {
System.out.println(
" method1finally... " );
return 5 ;
}
}
}

运行结果:

a==1
a==2
a==3
ArithmeticException...b==
mainfinally...
ArithmeticException...d==
method1finally...
end...c==5

如果把main函数中的int b=5/0放到前面,如注释掉的那部分:

结果如下:

ArithmeticException...b==
mainfinally...
ArithmeticException...d==
method1finally...
end...c==5

结论:(1)如果try块中,有多个语句都有可能发生异常,那么如果前面的代码发生了异常,不管该异常是不是被正确捕捉,try块这个最早发生异常的这个语句“后面的语句”将不会被执行到。

(2)不管有没有在try,catch,finally中有没有加return,也不管有没有发生异常,(只要没有System.exit()),程序总会执行到finally{}语句块“中”!!!

程序2:

package com.trycatch.test2;

public class TryCatchTest2 {
public static void main(String[] args) throws Exception {
int a[] = { 1 , 2 , 3 };
try {

for ( int i = 0 ; i < a.length; i ++ ) {
System.out.println(
" a== " + a[i]);
}

int b = 5 / 0 ;
/* if (true) {
while (true) {
throw new Exception("eeeee");
}
}
*/
}
catch (Exception e) {
throw new Exception( " eeeeee " + e.getMessage());
// 同理,throw 后面的这句将不会被执行到!
// System.out.println("error,but i did it");
} finally {
System.out.println(
" finally " );
}

System.out.println(
" end... " );

}
}

运行结果:

a==1
a==2
a==3
finally
Exception in thread "main" java.lang.Exception: eeeeee/ by zero
 at com.trycatch.test2.TryCatchTest2.main(TryCatchTest2.java:19)

有时候这个异常打印在第一句,此中,catch并没有处理这个异常,而是把这个异常抛出到它的上一层程序(也就是调用它的方法中)。

程序3:

package com.trycatch.test7;

public class TryCatchTest7 {
public static void main(String[] args) throws Exception {

TryCatchTest7 t3
= new TryCatchTest7();
// t3.method1(); // 如果没有把这个方法包含到try方法中,当这个方法发生异常的时候,自然不会被catch到,也就是这个main 方法下面的trycatchfinally不会被执行到。
try {
t3.method1();
// method1()的异常已经被正确捕获并且处理,所以会继续执行try块中下面的语句。
System.out.println( " t3zhixingwan.... " );
}
catch (Exception e){
System.out.println(
" mainexception " );
}
finally {
System.out.println(
" mainfinally... " );
}
System.out.println(
" mainend... " );
}

public void method1() throws Exception {
int a[] = { 1 , 2 , 3 };
try {
method2();
// 如果method2的异常没有处理,执行完method2就会直接到catch里面执行,不会执行下面的数组打印。
for ( int i = 0 ; i < a.length; i ++ ) {
System.out.println(
" a== " + a[i]);
}
}
catch (Exception e) {
// 如果catch正确捕获并且处理了这个异常,那么finally后面的语句会被执行到。
System.out.println( " error,but i did it " );
}
finally {
System.out.println(
" finally " );
}

System.out.println(
" end... " );
}
public void method2(){
try {
System.out.println(
" method2.... " );
int c = 5 / 0 ;
}
catch (NullPointerException e){
// 如果不写任何东西,是大忌!
e.printStackTrace();
}

}
}

运行结果:

method2....
error,but i did it
finally
end...
t3zhixingwan....
mainfinally...

程序4:

package com.trycatch.test8;

public class TryCatchTest8 {
public static void main(String[] args) throws Exception {

TryCatchTest8 t3
= new TryCatchTest8();
// t3.method1(); // 如果没有把这个方法包含到try方法中,当这个方法发生异常的时候,自然不会被catch到,也就是这个main 方法下面的trycatchfinally不会被执行到。
try {
t3.method1();
// t3.method1();的异常没有被正确处理,所以此处还是有异常,
// 下面这句将不会被执行到,而是跳转到catch里面去捕获method1()的异常。
System.out.println( " t3zhixingwan.... " );
}
catch (Exception e){
// 1、如果处理了这个异常,如下面这句,那么就会执行finally后面的语句。
System.out.println( " mainexception " );
// 2、如果没有对异常处理,而是抛出了这个异常,那么就会一直抛给main方法,main方法会抛给虚拟机,虚拟机会崩溃。
// throw new Exception("hhhhhhhhhh"); // 与throw e区别?
}
finally {
System.out.println(
" mainfinally... " );
}
System.out.println(
" mainend... " );
}

public void method1() throws Exception {
int a[] = { 1 , 2 , 3 };
try {
method2();
// method2()的异常被正确捕获,继续往下执行,下面这句出现数组范围越界异常。
for ( int i = 0 ; i < 4 ; i ++ ) {
System.out.println(
" a== " + a[i]);
}
}
catch (Exception e) {
// throw new Exception("eeeeee"+e.getMessage());
throw e;

}
finally {
System.out.println(
" finally " );
}
// 没有catch没有处理,而是抛出去了这个异常,如果所以下面这个打印end不会被执行。
System.out.println( " end... " );
}
public void method2(){
try {
System.out.println(
" method2.... " );
int c = 5 / 0 ;
}
catch (NullPointerException e){
// 如果不写任何东西,是大忌!
e.printStackTrace();
}
catch (ArithmeticException e){
e.printStackTrace();
}

}
}

运行结果:

method2....
java.lang.ArithmeticException: / by zero
 at com.trycatch.test8.TryCatchTest8.method2(TryCatchTest8.java:46)
 at com.trycatch.test8.TryCatchTest8.method1(TryCatchTest8.java:28)
 at com.trycatch.test8.TryCatchTest8.main(TryCatchTest8.java:9)//这句是method2()中打印出来的异常
a==1
a==2
a==3
finally
mainexception
mainfinally...
mainend...

程序5:

package com.trycatch.test9;

public class TryCatchTest9 {
public static void main(String[] args) throws Exception {

TryCatchTest9 t3
= new TryCatchTest9();
// t3.method1(); // 如果没有把这个方法包含到try方法中,当这个方法发生异常的时候,自然不会被catch到,也就是这个main 方法下面的trycatchfinally不会被执行到。
try {
t3.method1();
// t3.method1();的异常没有被正确处理,所以此处还是有异常,
// 下面这句将不会被执行到,而是跳转到catch里面去捕获method1()的异常。
System.out.println( " t3zhixingwan.... " );
}
catch (Exception e){
// 1、如果处理了这个异常,如下面这句,那么就会执行finally后面的语句。
System.out.println( " mainexception " );
// 2、如果没有对异常处理,而是抛出了这个异常,那么就会一直抛给main方法,main方法会抛给虚拟机,虚拟机会崩溃。
// throw new Exception("hhhhhhhhhh"); // 与throw e区别?
}
finally {
System.out.println(
" mainfinally... " );
}
System.out.println(
" mainend... " );
}

public void method1() throws Exception{
int a[] = { 1 , 2 , 3 };
try {
method2();
// method2()的异常没有被正确捕获,下面不会被执行。
for ( int i = 0 ; i < 4 ; i ++ ) {
System.out.println(
" a== " + a[i]);
}
}
catch (Exception e) {
// throw new Exception("eeeeee"+e.getMessage());
throw e;

}
finally {
System.out.println(
" finally " );
}
// 没有catch没有处理,而是抛出去了这个异常,如果所以下面这个打印end不会被执行。
System.out.println( " end... " );
}
public void method2() {
try {
System.out.println(
" method2.... " );
int c = 5 / 0 ;
}
catch (NullPointerException e){
// 如果不写任何东西,是大忌!
e.printStackTrace();
}
catch (ArithmeticException e){
throw e;
}
catch (Exception e){
e.printStackTrace();
}


}
}

运行结果:

method2....
finally
mainexception
mainfinally...
mainend...
程序6:

如果程序5中main()方法也没有处理这个异常,而是又抛出了这个异常,即throw new Exception("hhhhhhhhhh");

那么执行结果如下:

method2....
finally
mainfinally...
Exception in thread "main" java.lang.Exception: hhhhhhhhhh
 at com.trycatch.test9.TryCatchTest9.main(TryCatchTest9.java:17)

结论3:如果方法中的一个语句抛出一个没有在相应的trycatch块中处理的异常,如:method1()中抛出的 throw e;那么这个异常就会被抛出到它的调用方法中,如果异常也没有在调用方法中被处理,它就被抛出到该方法的调用程序,这个过程要一直延续到异常被处理。如果异常到这时还没有被处理,它便回到main(),而且,即使main()方法不处理它,就会抛给虚拟机,虚拟机就会崩溃,该异常就异常地中断程序。

关于finally后面的代码会不会被执行到?

结论4:如果异常被正确捕获并且处理,会接着finally下面的代码继续执行。

结论5:a、如果没有正确捕获该异常(如异常类型不匹配),

          b、或者正确捕获异常类型,但是没有对异常做处理,而是再次抛给其他程序

   以上a、b这两种情况都不会再执行finally后面的语句和try块该方法后面的语句(见结论1)。

程序7:throw e与throw new Exception();区别

package com.trycatch.test10;

public class TryCatchTest10 {
public static void main(String[] args) throws Exception {

TryCatchTest10 t3
= new TryCatchTest10();
try {
t3.method1();
// t3.method1();的异常没有被正确处理,所以此处还是有异常,
// 下面这句将不会被执行到,而是跳转到catch里面去捕获method1()的异常。
System.out.println( " t3zhixingwan.... " );
}
catch (Exception e){
// 2、如果没有对异常处理,而是抛出了这个异常,那么就会一直抛给main方法,main方法会抛给虚拟机,虚拟机会崩溃。
// throw new Exception("hhhhhhhhhh"); // 与throw e区别?
throw e;
}
finally {
System.out.println(
" mainfinally... " );
}
System.out.println(
" mainend... " );
}

public void method1() throws Exception{
int a[] = { 1 , 2 , 3 };
try {
method2();
// method2()的异常没有被正确捕获,下面不会被执行。
for ( int i = 0 ; i < 4 ; i ++ ) {
System.out.println(
" a== " + a[i]);
}
}
catch (Exception e) {
throw new Exception( " eeeeee " );

}
finally {
System.out.println(
" finally " );
}
// 没有catch没有处理,而是抛出去了这个异常,如果所以下面这个打印end不会被执行。
System.out.println( " end... " );
}
public void method2() {
try {
System.out.println(
" method2.... " );
int c = 5 / 0 ;
}
catch (NullPointerException e){
// 如果不写任何东西,是大忌!
e.printStackTrace();
}
catch (ArithmeticException e){
throw e;
}
catch (Exception e){
e.printStackTrace();
}


}
}

结论6:

如果在method2()中throw e,

               |

               |

              \/

         method1()中 throw new Exception("eeeeee");,

               |

               |

              \/

main()中throw e,看下面运行结果

运行结果:

也就是会把你加进去的“eeeeee”这段字符串加到打印到异常中。这就是区别。

如果在这三个方法里面都用throw e,那么打印结果如下:

也就是会把异常类型直接打印出来。然后提示你method2,1,main这三个方法中都发生了异常。

以上内容都是从java私塾中学来,加上自己的理解。

 

 

throw与throws的区别:(加总结)

package com.trycatch.test1;

public class ThrowsTest1 {
public static void main(String args[]) {
// throws Exception {//验证2、
try {
t1(); // 4、t1()方法有异常回来,到catch中,
} catch (Exception e) {// 5、类型匹配,
System.out.println("$$$$$" + e.getMessage());// 6、打印出了此异常消息
// throw e;//验证2、main()也不处理,继续抛,抛给了虚拟机
} finally {
System.out.println("finally");
}
}

private static void t1() throws Exception {
try {
t2();// 2、t2()回来之后有异常,到这里的catch中,类型匹配。(不会执行下面的打印)
System.out.println("$$$$$$$$$$$$$to end of t1");
} catch (Exception e) {
throw e;// 主动抛出后,方法体必须声明这个异常!先有throw,再有throws,throw在里,throws在外
// 3、类型虽然匹配,但是没有处理,又把这个异常抛给了他的调用者main方法
}
}

private static void t2() {
try {
int a = 4 / 0;// 1、调到t2()时,catch类型不匹配,没有catch住,抛给t2()的调用者t1()
} catch (NullPointerException e) {// 在自己的try里面发生的异常,在自己的catch里面把它catch住
e.printStackTrace();// 注意:不会到此执行,因为类型不匹配!!!

}
}
}

 

运行结果:

$$$$$/ by zero
finally

理解:

(1)用try ,catch“捕获”(不管有没有实际捕获住),的都是编译期的异常,在编译期能预见的异常。不是运行期异常!

(2)处理例外有两种,一种用try ,catch真正捕获住了(只要catch类型匹配,catch{方法里面没有throw;}),另一种没有处理,捕获住,而是throws,

(3)在方法内部用throw, 有了throw,方法声明那必须加throws这个throw出来的>=的异常。(有throw,必有throws,throw在内,throws在外)

 

 

验证加了return之后,finally是否执行

package com.trycatch.test1;

public class ThrowsTest2 {
public static void main(String args[]) {
try {
t1();
} catch (Exception e) {
System.out.println("$$$$$" + e.getMessage());
} finally {
System.out.println("1111finally");//4、接着finally
}
}

private static void t1() throws Exception {
try {
t2();
System.out.println("$$$$$$$$$$$$$to end of t1");//3、接着打印
} catch (Exception e) {
throw e;
}
}

private static void t2() {
try {
int a = 4 / 0;// 方法1、1、调到t2()时,catch类型匹配,catch住,
//int a = 4/1;//方法2:没有发生异常
return;
} catch (Exception e) {// 在自己的try里面发生的异常,在自己的catch里面把它catch住
e.printStackTrace();// 2、打印异常
System.out.println("#########t2");
}finally{
System.out.println("22222finally");
}
}
}

方法1的执行结果:

java.lang.ArithmeticException: / by zero
 at com.trycatch.test1.ThrowsTest2.t2(ThrowsTest2.java:25)
 at com.trycatch.test1.ThrowsTest2.t1(ThrowsTest2.java:16)
 at com.trycatch.test1.ThrowsTest2.main(ThrowsTest2.java:6)
#########t2
22222finally
$$$$$$$$$$$$$to end of t1
1111finally

 

方法2的执行结果

22222finally
$$$$$$$$$$$$$to end of t1
1111finally

不管加不加return,对trycatchfinally没有影响!

转载于:https://www.cnblogs.com/snowdrop/articles/2025805.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值