자바 튜토리얼

자바 홈 자바 소개 자바 시작하기 자바 구문 자바 주석 자바 변수 자바 데이터 유형 자바 타입 캐스팅 자바 연산자 자바 문자열 자바 수학 자바 부울 자바 If...Else 자바 스위치 자바 while 루프 자바 For 루프 자바 중단/계속 자바 배열

자바 메소드

자바 메소드 자바 메소드 매개변수 자바 메소드 오버로딩 자바 범위 자바 재귀

자바 클래스

자바 OOP 자바 클래스/객체 자바 클래스 속성 자바 클래스 메소드 자바 생성자 자바 수정자 자바 캡슐화 자바 패키지 / API 자바 상속 자바 다형성 자바 내부 클래스 자바 추상화 자바 인터페이스 자바 열거형 자바 사용자 입력 자바 날짜 자바 배열 목록 자바 링크드리스트 자바 해시맵 자바 해시셋 자바 반복자 자바 래퍼 클래스 자바 예외 자바 정규식 자바 스레드 자바 람다

자바 파일 처리

자바 파일 자바 파일 생성/쓰기 자바 읽기 파일 자바 삭제 파일

자바 방법

두 개의 숫자 더하기

자바 참조

자바 키워드 자바 문자열 메소드 자바 수학 메소드

자바 예제

자바 예제 자바 컴파일러 자바 연습 자바 퀴즈 자바 인증서


Java 가 키워드 를 던졌습니다 .

❮ 자바 키워드


예시

연령 이 18세 미만인 경우 예외를 throw합니다 ("액세스 거부됨" 인쇄). 연령이 18세 이상인 경우 "접근 허가됨"을 인쇄하십시오.

public class Main {
  static void checkAge(int age) throws ArithmeticException {
    if (age < 18) {
      throw new ArithmeticException("Access denied - You must be at least 18 years old.");
    }
    else {
      System.out.println("Access granted - You are old enough!");
    }
  }

  public static void main(String[] args) {
    checkAge(15); // Set age to 15 (which is below 18...)
  }
}


정의 및 사용

키워드 는 throws메서드에서 throw될 수 있는 예외 유형을 나타냅니다.

ArithmeticExceptionJava 에는 , ClassNotFoundException, ArrayIndexOutOfBoundsException, SecurityException많은 예외 유형이 있습니다 .

throw의 차이점 throws:

throw throws
Used to throw an exception for a method Used to indicate what exception type may be thrown by a method
Cannot throw multiple exceptions Can declare multiple exceptions
Syntax:
  • throw is followed by an object (new type)
  • used inside the method
Syntax:
  • throws is followed by a class
  • and used with the method signature

관련 페이지

Java Try..Catch Tutorial 에서 예외에 대해 자세히 읽어보십시오 .


❮ 자바 키워드