Java throws 使用

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

 

在开发中,若是去调用别人写的方法时,是否能知作别人写的方法是否会发生异常?这是很难判断的。针对这种状况,Java总容许在方法的后面使用throws关键字对外声明该方法有可能发生异常,这样调用者在调用方法时,就明确地知道该方法有异常,而且必须在程序中对异常进行处理,不然编译没法经过。ide

以下面代码.net

 

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处理debug

 

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 运行程序blog

 

当 调用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运行程序get

 

 

 

 

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

1. 编译时异常io

   在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

相关文章
相关标签/搜索