public class ExceptionTest6 {
public static void main(String[] args) {
User1 ins =new User1();
try {
ins.prt(2);
}catch(UserException e)
{
System.out.println(e);
}finally {
System.out.println("finally");
}
}
}
//try catch문으로 exception을 날린다.
public class User1 {
public void prt(int su) throws UserException
{
if(su<2)
throw new UserException("2보다 작습니다.");
System.out.println("입력된 숫자는"+ su);
}
}
//throws로 exception을 날린다.
import javax.print.DocFlavor.STRING;
public class UserException extends Exception{//직접 예외를 생성하는 방법 조상클래스를 상속받는다.
public UserException(String str)
{
super(str);// 부모클래스인 exception의 생성자를 호출 하겠다
}
public UserException()
{
super("2보다 작습니다.");// 출력값을 직접 입력할때
}
}
//사용자생성exception
'프로그래밍 > java' 카테고리의 다른 글
LocalDate, LocalTime 시간 비교 (0) | 2023.10.17 |
---|---|
String , StringBuffer, StringBuilder (1) | 2019.11.13 |
Iterator (1) | 2019.11.13 |
Hashtable 객체 출력에서 생기는 오류 (2) | 2019.11.13 |