package com.basic; //예외처리 throws 사용법 import java.io.*; //모든 클래스를 사용하겠다는 것임(좋은 표현은 아님) public class ThrowsEx1 { private void printDate() throws NumberFormatException,IOException{ System.out.print("단 입력 :"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int dan = Integer.parseInt(br.readLine()); System.out.println(dan+"단"); System.out.println("---------"); for(int i=1;i<=9;i++) System.out.println(dan+"*"+i+"="+(dan*i)); } public static void main(String[] args){ //throws에 에러 값을 가지고 있다가 밑에 출력부분에서 try catch에서 수행됨 ThrowsEx1 t1 = new ThrowsEx1(); try{ t1.printDate(); }catch(Exception e){ System.out.println("숫자가 아닙니다."); } } } //readLine은 IOException 예외가 발생하기 때문에 의무적으로 꼭 예외처리를 해야됨
단 입력 :4
4단
---------
4*1=4
4*2=8
4*3=12
4*4=16
4*5=20
4*6=24
4*7=28
4*8=32
4*9=36
'Java > Exception Handing' 카테고리의 다른 글
사용자 정의 예외 (0) | 2012.04.11 |
---|---|
예외처리 throw사용법 (0) | 2012.04.11 |
예외처리 다중 catch문 사용하기 (0) | 2012.04.11 |
예외처리 finally 사용법 (0) | 2012.04.11 |
예외처리 5 (0) | 2012.04.11 |