zoukankan      html  css  js  c++  java
  • thows,thow和try catch的区别

    1、throw是当前方法不处理这个异常,由它的上一级进行处理。并且抛出异常后将停止执行代码。

    package myProject;
    
    public class ExceptionTest {
        //测试throw
        public void testThrow() {
            try {            
                int a=1/0;
                
            }catch(Exception e){
                System.out.println("1");
                throw new RuntimeException();
            }finally {
                System.out.println("2");
            }
            System.out.println("3");
        }
        public static void main(String[] args) {
            ExceptionTest t=new ExceptionTest();
            t.testThrow();
        }
    }

    输出结果为:

    1
    Exception in thread "main" 2
    java.lang.RuntimeException
        at myProject.ExceptionTest.testThrow(ExceptionTest.java:11)
        at myProject.ExceptionTest.main(ExceptionTest.java:20)

    可见,没有打印3,即throw抛出异常后,会执行finally块的代码,但不会再执行后边的代码。调用这种方法时,可以用try catch捕获并处理这个异常,并用finally块达到输出3的目的,见如下代码:

    package myProject;
    
    public class ExceptionTest {
        //测试throw
        public void testThrow() {
            try {            
                int a=1/0;
                
            }catch(Exception e){
                System.out.println("1");
                throw new RuntimeException();
            }finally {
                System.out.println("2");
            }
            System.out.println("3");
        }
        public void test1() {
            try {
                testThrow();
            }catch(Exception e) {
                e.printStackTrace();
            }finally{
           System.out.println("3");
         } }
    public static void main(String[] args) { ExceptionTest t=new ExceptionTest(); t.test1(); } }

    输出结果为:

    1
    2
    java.lang.RuntimeException
    3
     at myProject.ExceptionTest.testThrow(ExceptionTest.java:11)
     at myProject.ExceptionTest.test1(ExceptionTest.java:19)
     at myProject.ExceptionTest.main(ExceptionTest.java:28)

    2、try catch 是直接处理异常,执行完finally块后,接着执行代码。

    package myProject;
    
    public class ExceptionTest {
        //测试try catch
        public void testCatch() {
            try {            
                int a=1/0;
                
            }catch(Exception e){
                System.out.println("1");
            }finally {
                System.out.println("2");
            }
            System.out.println("3");
        }
        public void test1() {
            try {
                testCatch();
            }catch(Exception e) {
                System.out.println("4");
                e.printStackTrace();
            }finally {
                System.out.println("5");
            }
        }
        public static void main(String[] args) {
            ExceptionTest t=new ExceptionTest();
            t.test1();
            
        }
    }

    输出结果如下:

    1
    2
    3
    5

    可见,由于testCatch()已经用try catch处理了异常,那么在test1()方法中的catch块将不会执行,也就不会输出4

    3、throws写在方法参数的后边,声明了该方法有可能抛出异常。如果这个方法的确有可能会抛出一个异常,那么编辑器会强制你加上这个throws,见如下代码

    package myProject;
    
    public class ExceptionTest {
        //测试try catch
        public void testCatch() throws Exception{        
            int a=1/0;    
        }
        public void test() {
            try {
                testCatch();
            }catch(Exception e) {
                System.out.println("1");
            }finally {
                System.out.println("2");
            }
        }
        public static void main(String[] args) {
            ExceptionTest t=new ExceptionTest();
            t.test();
        }
    }

    输出结果如下:

    1
    2

    可见,try catch可以捕获有带throws的方法的异常。

    4、try catch 可以捕获try catch捕获的异常

    见如下代码:ExceptionTest2类

    package myProject;
    
    public class ExceptionTest2 {
        public void trycatch() {
            try {
                int i=1/0;
            }catch(Exception e){
                System.out.println("ExceptionTest2-----catch");
                e.printStackTrace();
            }
        }
    }

    ExceptionTest类:

    package myProject;
    
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    
    public class ExceptionTest {
        private static final Logger logger=LogManager.getLogger(); 
        private ExceptionTest2 t;
        public void  test() {
            try {
                t.trycatch();
                
            }catch(Exception e){    
                System.out.println("ExceptionTest----catch"); 
                e.printStackTrace();
            }
        }
        public static void main(String[] args) {
            ExceptionTest test= new ExceptionTest();
            test.test();
            
        }
        
    }

    ExceptionTest类中的test()方法调用ExceptionTest2类trycatch()方法,所以test()会捕获trycatch()捕获的异常

    输出结果为:

    ExceptionTest----catch
    java.lang.NullPointerException
        at myProject.ExceptionTest.test(ExceptionTest.java:11)
        at myProject.ExceptionTest.main(ExceptionTest.java:21)
  • 相关阅读:
    Asp.Net MVC 2 RC 2 发布
    SqlServer2008修改表时出现“save changes is not permitted…”解决方法
    使用LoadRunner测试WMS
    使用Expression Encoder 3发布媒体文件到WebDAV
    Net Remoting Error:试图创建未绑定类型的代理
    Net4.0VS2010新特性
    WCF的一些基本知识点
    WCF中的ServiceHost初始化两种方式
    WMS中添加默认发布点
    HTTP 错误 500.19 Internal Server Error 错误解决方法
  • 原文地址:https://www.cnblogs.com/BonnieWss/p/9225936.html
Copyright © 2011-2022 走看看