English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
프로그램 실행 중 발생하는 문제(실행 시간 오류)는 예외입니다. 예외가 발생하면 프로그램이 갑자기 종료되고, 예외가 발생한 코드 행 이후의 코드는 결코 실행되지 않습니다.
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包含两个私有变量名称age和一个初始化实例变量的参数化构造函数。
作为主要方法,我们接受用户的姓名和年龄值,并通过传递接受的值来初始化Student类。
在Student类的构造函数中,我们创建了一个异常AgeDoesnotMatchException的对象,并在年龄值介于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 하고 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("Student의 이름: "+this.name ); System.out.println("Student의 연령: "+this.age ); } public static void main(String args[]) { Scanner sc= new Scanner(System.in); System.out.println("Student의 이름을 입력해야 합니다: "); String name = sc.next(); System.out.println("Student의 연령을 입력해야 합니다 17 까지 24 (포함하여 17 하고 24): "); int age = sc.nextInt(); Student obj = new Student(name, age); obj.display(); } }
출력 결과
이 프로그램을 실행할 때, 키보드에서 이름과 연령 값을 전달해야 합니다. 주어진 연령 값이 다음 범위에 포함되지 않으면17까지24이 범위 사이가 아니면 예외가 발생합니다. 예를 들어-
Student의 이름을 입력해야 합니다: Krishna Student의 연령을 입력해야 합니다 17 까지 24 (포함하여 17 하고 24) 14 AgeDoesnotMatchException: 연령은 다음 범위 사이가 아닙니다 17 하고 24 Student의 이름: Krishna' Student의 연령 14 at Student.<init>(Student.java:18) at Student.main(Student.java:39)