개발모음집

JAVA> error: unreported exception ParseException 본문

JAVA

JAVA> error: unreported exception ParseException

void 2018. 4. 18. 09:00

에러



# javac ChattingServer.java


ChattingServer.java:179: error: unreported exception ParseException; must be caught or declared to be thrown
toClientObject = toClientParser.parse(userNoMsg);
^
1 error

수정 전 코드


toClientParser = new JSONParser();
toClientObject = toClientParser.parse(userNoMsg);
toClientJsonObject = (JSONObject) toClientObject;


Json을 파싱해주는 코드를 추가하고 컴파일 했을 때  ParseException 을 선언하라는 에러가 발생했다.

결론부터 말하자면 "ParseException"이 아니라 "Exception"으로 처리를 해줘야한다.

에러 해결 코드
try {
toClientParser = new JSONParser();
toClientObject = toClientParser.parse(userNoMsg);
toClientJsonObject = (JSONObject) toClientObject;
} catch (Exception e) {
e.printStackTrace();
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

해결 과정


에러 발생후 PasreException을 해줬더니  ParseException이 동일하게 발생하였다.

수정 후 코드 try {
toClientParser = new JSONParser();
toClientObject = toClientParser.parse(userNoMsg);
toClientJsonObject = (JSONObject) toClientObject;
} catch (ParseException e) {
e.printStackTrace();
}


ParseException 해준 결과 

ChattingServer.java:188: error: exception ParseException is never thrown in body of corresponding try statement
} catch (ParseException e) {
^
ChattingServer.java:180: error: unreported exception ParseException; must be caught or declared to be thrown
toClientObject = toClientParser.parse(userNoMsg);
^
2 errors


그래서 Exception 해준 결과 에러가 해결되었다.


에러 해결 코드
try {
toClientParser = new JSONParser();
toClientObject = toClientParser.parse(userNoMsg);
toClientJsonObject = (JSONObject) toClientObject;
} catch (Exception e) {
e.printStackTrace();
}