본문 바로가기

[java] 사용자 정의 예외 클래스

반응형

import java.util.Scanner;

   

class NewException extends Exception

{

  String reason;

  public NewException(String reason)

  {

    this.reason = reason;

      

  }

    

  public String toString()

  {

    return reason;

  }

    

}

   

   

public class ExcepTest5

{

  public static void method() throws Exception

  {

    throw new NewException(사용자 정의 예외 발생");

  }

  public static void main(String[] args)

  {

    try

    {

      method();

    }

    catch(Exception e)

    {

      System.out.println(e);

    }

    System.out.println("예외처리를 하였기에 정상 종료함");

  }

}

반응형
-->