Java 中程序员如何手动抛出异常?
异常是在程序执行期间发生的错误(运行时错误)。当发生异常时,程序会突然终止,并且异常行之后的代码将不再执行。
示例
import java.util.Scanner;
public class ExceptionExample {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number: ");
int a = sc.nextInt();
System.out.println("Enter second number: ");
int b = sc.nextInt();
int c = a/b;
System.out.println("The result is: "+c);
}
}输出
Enter first number: 100 Enter second number: 0 Exception in thread "main" java.lang.ArithmeticException: / by zero at ExceptionExample.main(ExceptionExample.java:10)
手动抛出异常
您可以使用 *throw* 关键字显式地抛出一个用户定义的异常或预定义的异常。
异常有两种类型:用户定义和预定义,每个异常都由一个类表示,该类继承自 Throwable 类。
要显式抛出异常,您需要实例化该类的对象,并使用 throw 关键字抛出该对象。
示例
以下 Java 程序抛出 NullPointerException 异常
public class ExceptionExample {
public static void main(String[] args) {
System.out.println("Hello");
NullPointerException nullPointer = new NullPointerException();
throw nullPointer;
}
}输出
Hello Exception in thread "main" java.lang.NullPointerException at MyPackage.ExceptionExample.main(ExceptionExample.java:6)
每当您显式抛出异常时,您都需要确保包含 throw 关键字的行是程序的最后一行。这是因为在它之后编写的任何代码都是不可达代码,如果在此行下面仍然有代码片段,则会生成编译时错误。
示例
public class ExceptionExample {
public static void main(String[] args) {
System.out.println("Hello");
NullPointerException nullPointer = new NullPointerException();
throw nullPointer;
System.out.println("How are you");
}
}编译时错误
D:\>javac ExceptionExample.java
ExceptionExample.java:6: error: unreachable statement
System.out.println("How are you");
^
1 error用户定义异常
通常,throw 关键字主要用于抛出用户定义的异常。每当您需要定义自己的异常时,您都需要定义一个扩展 Throwable 类的类,并覆盖所需的方法。
实例化此类,并在您希望抛出异常的任何位置使用 throw 关键字抛出它。
示例
在以下 Java 程序中,我们创建了一个名为 AgeDoesnotMatchException 的自定义异常类。
public class AgeDoesnotMatchException extends Exception{
AgeDoesnotMatchException(String msg){
super(msg);
}
}另一个类 Student 包含两个私有变量 name、age 和一个参数化构造函数,用于初始化实例变量。
从 main 方法中,我们从用户那里接受 name 和 age 值,并通过传递接受的值来初始化 Student 类。
在 Student 类的构造函数中,我们创建了一个异常 **AgeDoesnotMatchException** 的对象,并在 age 值介于 17 和 24 之间时引发异常(使用 throws)。
public class Student extends RuntimeException {
private String name;
private int age;
public Student(String name, int age){
try {
if (age<17||age>24) {
String msg = "Age is not between 17 and 24";
AgeDoesnotMatchException ex = new AgeDoesnotMatchException(msg);
throw ex;
}
}catch(AgeDoesnotMatchException e) {
e.printStackTrace();
}
this.name = name;
this.age = age;
}
public void display(){
System.out.println("Name of the Student: "+this.name );
System.out.println("Age of the Student: "+this.age );
}
public static void main(String args[]) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter the name of the Student: ");
String name = sc.next();
System.out.println("Enter the age of the Student should be 17 to 24 (including 17 and 24): ");
int age = sc.nextInt();
Student obj = new Student(name, age);
obj.display();
}
}输出
执行此程序时,您需要从键盘传递 name 和 age 值。如果给定的 age 值不在 17 和 24 之间,则会发生异常,如下所示:
Enter the name of the Student: Krishna Enter the age of the Student should be 17 to 24 (including 17 and 24): 14 AgeDoesnotMatchException: Age is not between 17 and 24 Name of the Student: Krishna' Age of the Student: 14 at Student.<init>(Student.java:18) at Student.main(Student.java:39)
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP