본문 바로가기

Study/Java

예외처리, Exception



import java.io.*;

class EXC2
{
        public static void main(String args[]) throws IOException
        {
                String temp_num;
                int number;
                BufferedReader Buffer;;
                Buffer = new BufferedReader(new InputStreamReader(System.in));
                try
                {
                        System.out.print("only number : ");
                        temp_num=Buffer.readLine();
                        number=Integer.parseInt(temp_num);
                        System.out.println("number is "+number);
                }
                catch(NumberFormatException e)
                {
                        System.out.println("wrong information");

                }
                finally
                {
                        System.out.println("have noting to do");
                }
        }
}


C++에서는 예외처리가 있다고는 알고 있습니다. 아직 이쪽으로는 ++까지는 안해봐서,,
약간의 기억을 더듬자면, C로 switch case 나 또는 if를 이용해서 메뉴를 입력받을때, 물론 정상적인 시나리오로는
작동이 되겠지만..입력해서 안될 값을 입력하면 segement fault 제대로 입력했다면, 그냥 죽어버립니다..

물론, 여러가지 프로그래밍 기법들은 통해서 해결하는 방법은 있겠지만..자바를 처음 배우는 입장에서는 왠지 모를
그런것이랄까...;?

잠깐 해석하면 try는 에러가 발생할 범위이고, catch는 에러가 나면 이렇게 처리하고
finally는 에러나든지 말든지 출력합니다.