도리안의 개발 이야기 #59 - 자바의 예외 관련 자료

in #kr-dev5 years ago (edited)


IMAGE 2019-02-04 23:45:15.jpg

대문 제작: imrahelk

image.png

지금까지 자바 코드에서 예외(exception) 발생시 주로 처리하는 것은 스택 트레이스를 콘솔에 출력하는 것이었죠. 그러면 로그에 남으니 나중에 그것을 보고 어느 코드에서 예외가 발생했는지 찾을 수 있고 무엇이 원인인지 분석할 수 있습니다. 한편, 서버에서 발생하는 예외를 데이터베이스에 저장하면 좋지 않을까 생각 중입니다. 그러면 테이블과 컬럼을 정의해야 하는데, 예외 관련 자료가 무엇이 있는지 알아야 합니다. 이를 위해 간단한 자바 코드를 만들어 보았습니다.

예외 출력 코드

package lee.dorian.java.javatest;

public class ExceptionTest {

    public static void main(String[] args) {
        String str = "123a";
        try {
            int num = Integer.parseInt(str);            // NumberFormatException 발생
            System.out.println("num: " + num);
        }
        catch (Exception e) {
            System.out.println("Exception name: " + e.getClass().getName());
            System.out.println("Exception message: " + e.getMessage());
            System.out.println("Localized Exception message: " + e.getMessage());
            System.out.println("Stack trace: ");
            for (int i = 0; i < e.getStackTrace().length; i++) {
                StackTraceElement ste = e.getStackTrace()[i];
                System.out.println(ste.toString());
            }
        }
    }

}

실행 결과

Exception name: java.lang.NumberFormatException
Exception message: For input string: "123a"
Localised Exception message: For input string: "123a"
Stack trace: 
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
java.lang.Integer.parseInt(Integer.java:580)
java.lang.Integer.parseInt(Integer.java:615)
lee.dorian.java.javatest.ExceptionTest.main(ExceptionTest.java:8)

컬럼으로 추가할 수 있는 자료는 name, message, localized message, stack trace 등이 되겠습니다. 관련 테이블은 다음 포스트에서 보여드릴 예정입니다.

aaronhong_banner.jpg

천하제일연재대회 지난 글

36회 - 천하제일연재대회 시작! 그리고 예정 주제
38회 - 이직을 원하는 이유
39회 - 이직을 주저하는 이유
40회 - 일과 중에 학습 시간을 갖기로
41회 - 서비스 개발에 대한 회의감
42회 - 면접 후 생각
43회 - 천하제일연재대회 보팅 순서 관찰기
44회 - 포트폴리오 DB 계획 그리고 아쉬운 ERD 도구
45회 - 최근의 개발 업무의 실수 그리고 개선점
46회 - 개발 내용 기록에 대한 고민
47회 - 히스토리를 어떻게 남길까
48회 - 아쉬운 면접 결과
49회 - 개인 개발 주제 구상
50회 - 코드 복기 필요
51회 - 업무 분장 변경
52회 - 스프링 기반 restful API 개발 방법 정리 계획
53회 - 엇나간 개발 일정
54회 - 업무 분장 변경 하나? 안 하나?
55회 - 이력서 새로 작성
56회 - 헤드헌터와 통화
57회 - 대기업이냐 중소기업이냐
58회 - 서비스 안정화 필요

Sort:  

짱짱맨 호출에 응답하였습니다.

finally 추가하여 예외 처리 못할 경우 최종 어떤 액션을 취할지 코딩함도 좋습니드앙~♥♩♬

행복한 ♥ 오늘 보내셔용~^^

Posted using Partiko Android

Congratulations @dorian-dev! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You received more than 100 as payout for your posts. Your next target is to reach a total payout of 250

Click here to view your Board
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Do not miss the last post from @steemitboard:

Carnival Challenge - Collect badge and win 5 STEEM
Vote for @Steemitboard as a witness and get one more award and increased upvotes!

Coin Marketplace

STEEM 0.20
TRX 0.13
JST 0.029
BTC 65915.92
ETH 3486.15
USDT 1.00
SBD 2.67