亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> java

Java 抛出和抛出

Java 抛出和抛出

在本教程中,我们将通过示例学习使用 throw 和 throws 关键字进行异常处理。

在Java中,异常可以分为两类:

参考Java Exceptions详细了解checked和unchecked exceptions。

通常,我们不需要处理未经检查的异常。这是因为由于编程错误而发生未经检查的异常。而且,纠正它们而不是处理它们是一个好习惯。

本教程现在将重点介绍如何使用 throw 处理已检查的异常 和 throws .


Java throws 关键字

我们使用 throws 方法声明中的关键字来声明其中可能发生的异常类型。

它的语法是:

accessModifier returnType methodName() throws ExceptionType1, ExceptionType2 … {
  // code
}

从上面的语法可以看出,我们可以使用 throws 声明多个异常。


示例 1:Java 抛出关键字

import java.io.*;
class Main {
  public static void findFile() throws IOException {
    // code that may produce IOException
    File newFile=new File("test.txt");
    FileInputStream stream=new FileInputStream(newFile);
  }

  public static void main(String[] args) {
    try{
      findFile();
    } catch(IOException e){
      System.out.println(e);
    }
  }
}

输出

java.io.FileNotFoundException: test.txt (No such file or directory)

当我们运行这个程序时,如果文件test.txt 不存在,FileInputStream 抛出一个 FileNotFoundException 它扩展了 IOException 类。

如果一个方法不处理异常,则必须在throws中指定其中可能发生的异常类型 子句,以便调用堆栈中更靠前的方法可以处理它们或使用 throws 指定它们 关键字自己。

findFile() 方法指定 IOException 可以扔。 main() 方法调用该方法,如果抛出异常则处理。


抛出多个异常

以下是我们如何使用 throws 引发多个异常 关键字。

import java.io.*;
class Main {
  public static void findFile() throws NullPointerException, IOException, InvalidClassException {
    
    // code that may produce NullPointerException
    … … … 

    // code that may produce IOException
    … … … 

    // code that may produce InvalidClassException 
    … … … 
  }

  public static void main(String[] args) {
    try{
      findFile();
    } catch(IOException e1){
      System.out.println(e1.getMessage());
    } catch(InvalidClassException e2){
      System.out.println(e2.getMessage());
    }
  }
}

在这里,findFile() 方法指定它可以抛出 NullPointerException , IOException , 和 InvalidClassException 在其 throws 子句。

请注意,我们还没有处理 NullPointerException .这是因为它是一个未经检查的异常。不需要在throws中指定 子句并处理它。


抛出关键字 Vs.尝试...抓住...终于

可能有几种方法会导致异常。编写 try...catch 因为每种方法都会很乏味,代码也会变得冗长且可读性差。

throws 当您检查了不想在当前方法中捕获的异常(必须处理的异常)时也很有用。


Java throw 关键字

throw 关键字用于显式抛出单个异常。

当抛出异常时,程序执行流程从try转移 阻止到 catch 堵塞。我们使用 throw 方法中的关键字。

它的语法是:

throw throwableObject;

可抛出对象是类 Throwable 的实例 或 Throwable 的子类 类。


示例 2:Java throw 关键字

class Main {
  public static void divideByZero() {
    throw new ArithmeticException("Trying to divide by 0");
  }

  public static void main(String[] args) {
    divideByZero();
  }
}

输出

Exception in thread "main" java.lang.ArithmeticException: Trying to divide by 0
    at Main.divideByZero(Main.java:3)
    at Main.main(Main.java:7)
exit status 1

在这个例子中,我们明确地抛出了一个 ArithmeticException.

注意: ArithmeticException 是未经检查的异常。通常不需要处理未经检查的异常。


示例 3:抛出已检查异常

import java.io.*;
class Main {
  public static void findFile() throws IOException {
    throw new IOException("File not found");
  }

  public static void main(String[] args) {
    try {
      findFile();
      System.out.println("Rest of code in try block");
    } catch (IOException e) {
      System.out.println(e.getMessage());
    }
  }
}

输出

File not found

findFile() 方法抛出一个 IOException 使用我们传递给其构造函数的消息。

请注意,由于它是一个检查异常,我们必须在 throws 中指定它 子句。

调用此 findFile() 的方法 方法需要处理此异常或使用 throws 指定它 关键字自己。

我们已经在 main 中处理了这个异常 () 方法。程序执行流程从try转移 阻止到 catch 抛出异常时阻塞。因此,try 中的其余代码 块被跳过,catch 中的语句 块被执行。


java

  1. Java 变量和文字
  2. Java 基本输入和输出
  3. Java 表达式、语句和块
  4. Java 类和对象
  5. Java 抽象类和抽象方法
  6. Java 嵌套和内部类
  7. Java 捕获多个异常
  8. Java 自动装箱和拆箱
  9. Java String replace()、replaceAll() 和 replaceFirst() 方法
  10. Java String toLowercase() 和 toUpperCase() 方法
  11. Java - 对象和类
  12. Java - 日期和时间