zoukankan      html  css  js  c++  java
  • Java throws 使用

    copy from: https://blog.csdn.net/kangguang/article/details/79177336

    在开发中,如果去调用别人写的方法时,是否能知道别人写的方法是否会发生异常?这是很难判断的。针对这种情况,Java总允许在方法的后面使用throws关键字对外声明该方法有可能发生异常,这样调用者在调用方法时,就明确地知道该方法有异常,并且必须在程序中对异常进行处理,否则编译无法通过。

    如下面代码

    package www.kangxg.jdbc;

    public class Example {

        public static void main(String[] args) {
            // TODO Auto-generated method stub
           int result = divide(4,2);
           System.out.println(result);
        }
        
        public static int divide(int x,int y) throws Exception
        {
            int result = x/y;
            return result;
        }

    }
    这时候 编译器上会有错误提示  Unhandled exception type Exception
    所以需要对调用divide()方法进行try...catch处理

    package www.kangxg.jdbc;

    public class Example {

        public static void main(String[] args) {
            
          try {
             int    result = divide(4,2);
              System.out.println(result);
           } catch (Exception e) {
            
              e.printStackTrace();
           }
          
        }
        
        public static int divide(int x,int y) throws Exception
        {
            int result = x/y;
            return result;
        }

    }
    debug 运行程序

    当 调用divide()方法时,如果不知道如何处理声明抛出的异常,也可以使用throws 关键字继续抛异常,这样程序也能编译运行。但是注意的是,程序一旦发生异常,如果没有被处理,程序就会非正常终止。如下:

    package www.kangxg.jdbc;

    public class Example {

        public static void main(String[] args) throws Exception {
            
              int    result = divide(4,0);
              System.out.println(result);
          
        }
        
        public static int divide(int x,int y) throws Exception
        {
            int result = x/y;
            return result;
        }

    }
      

    debug运行程序

     

    Java 运行时异常与编译时异常

    1. 编译时异常

       在Java 中,Exception类中除了RuntimeException 类及其子类外都是编译时异常。编译时异常的特点是Java编译器会对其进行检查,如果出现异常就必须对异常进行处理,否则程序无法编译通过。

      处理方法

       使用try... catch 语句对异常进行捕获

       使用throws 关键字声明抛出异常,调用者对其进行处理

    2.运行时异常

      RuntimeException 类及其子类运行异常。运行时异常的特点是Java编译器不会对其进行检查。也就是说,当程序中出现这类异常时,即使没有使用try... catch 语句捕获使用throws关键字声明抛出。程序也能编译通过。运行时异常一般是程序中的逻辑错误引起的,在程序运行时无法修复。例如 数据取值越界。

    三 自定义异常

      JDK中定义了大量的异常类,虽然这些异常类可以描述编程时出现的大部分异常情况,但是在程序开发中有时可能需要描述程序中特有的异常情况。例如divide()方法中不允许被除数为负数。为类解决这个问题,在Java中允许用户自定义异常,但自定义的异常类必须继承自Exception或其子类。例子如下

    package www.kangxg.jdbc;

    public class DivideDivideByMinusException  extends Exception {
        
        /**
         *
         */
        private static final long serialVersionUID = 1L;

        public DivideDivideByMinusException(){
            super();
        }
        
        public DivideDivideByMinusException(String message)
        {
            super(message);
        }
    }
     

    package www.kangxg.jdbc;

    public class Example {

        public static void main(String[] args) throws Exception {
            
            try {
                 int    result = divide(4,-2);
                  System.out.println(result);
             } catch (DivideDivideByMinusException e) {
                
                 System.out.println(e.getMessage());
             }
          
        }
        
        public static int divide(int x,int y) throws DivideDivideByMinusException
        {
            if(y<0)
            {
                throw new DivideDivideByMinusException("被除数是负数");
            }
            int result = x/y;
            return result;
        }

    }
    ————————————————
    版权声明:本文为CSDN博主「清雨未尽时」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/kangguang/article/details/79177336

    Always Believe Something Beauitful Will Be Happen
  • 相关阅读:
    Window下安装redis
    Redhat安装python环境(readline模块)
    Golang之hello,beego
    Golang之go 命令用法
    Golang之Mysql事务
    Golang之waitgroup用法
    记录java版本不兼容的坑,(kafka运行报错)
    位运算的技巧(有拓展的技巧)
    关于单片机软件框架的一点思考
    解决main.o(.data) type RW incompatible with bsp.o(.ARM.__AT_0x24001000) type ZI in er RW_IRAM2.(转载)
  • 原文地址:https://www.cnblogs.com/Oude/p/12362452.html
Copyright © 2011-2022 走看看