-->

[Java] / [자바]

예외 발생 시키기

* 목표 : 고의로 예외처리를 만드는 이유와 방법에 대해 알아보자.

 

개인적으로 개발의 기본은 try catch라고 생각한다. 

예외처리 구문으로 감싸지 않은 코드에서 종류를 불문하고 에러가 발생한다면 개발자가 감당 할 수 없는 에러로 보이기 때문이다. 

예외처리를 통해 Exception이 발생할 수 있는 부분을 catch 해내고 그것에 대한 짧은 메세지라도 유저에게 출력해 보여주는 것은 개발자가 이 에러를 핸들링하고 있구나 라는 믿음을 줄 수 있다.

그리고 개발 단계에서 정상적으로 예외처리가 작동하는지 확인할 필요가 있다. 다음과 같은 코드로 말이다.

        try{
            //예외처리 발생 
            throw new Exception("error");
            //throw new RuntimeException();
            //throw new SQLiteException();
            
        } catch (Exception e){
            //예외처리 메세지 출력 
            //console.log(e);
            //System.print.out("e");
        }

 

고객정보를 REST 통신으로 서버에 업로드 하고 Response를 받아 처리하는 메소드이다. 

    private void uploadCustomers(ArrayList<Customer> customers) {
        if (!customers.isEmpty()) {

            cFunction.loading(getContext());

            Call<List<Customer>> call = SynchronizationFragment.skylarkService.insertCustomer(customers);

            call.enqueue(new Callback<List<Customer>>() {

                @Override
                public void onResponse(Call<List<Customer>> call, Response<List<Customer>> response) {
                    if (!response.isSuccessful()) {
                        Log.e(TAG, "uploadCustomers response !isSuccessful " );
                        return;
                    }

                    //Customer Return 받아서 CustomerNo update
                    try {
                        for (int i = 0; i < customers.size(); i++) {

                            Customer returnCustomer = response.body().get(i);
                            Log.w(TAG, "returnCustomer : " + returnCustomer);

                            String beforeCustomerNo = customers.get(i).getCustomerNo();
                            String AfterCustomerNo = returnCustomer.getCustomerNo();
                            String AfterTabletSyncSts = returnCustomer.getTabletSyncSts();

                            customers.get(i).setTabletSyncSts(AfterTabletSyncSts);
                            customers.get(i).setCustomerNo(AfterCustomerNo);

                            MyAppDatabase.getInstance().customerDao().updateCustomer(customers.get(i));

                            if (AfterTabletSyncSts.equals("99")){ //Error 발생!
                                Log.e(TAG, "customer Error code 99 !");
                                JSONObject jsonObject = new JSONObject(returnCustomer.toString());
                                String errMsg = jsonObject.get("errMsg").toString();
                                Log.e(TAG, "errMsg : " + errMsg);
                                cf.openPopupPad(getContext(), cFunction.POPUP_LOG_ERROR, errMsg);
                                throw new Exception(errMsg); //예외처리 만들기
                            }

                            Log.w(TAG, "Customer Update Finished [ beforeCustomerNo : " + beforeCustomerNo + " , AfterCustomerNo : " + AfterCustomerNo + " ]");

                        }

                        cf.openPopupPad(getContext(), cFunction.POPUP_LOG_OK, strLog);

                    } catch (Exception e) {
                        Log.w(TAG, "upload customer and add return customer response Error!", e);
                        cf.openPopupPad(getContext(), cFunction.POPUP_LOG_ERROR, e.toString());
                    }

                    cFunction.updateSyncDate(new Date());
                    cFunction.loadingEnd();

                }

                @Override
                public void onFailure(Call<List<Customer>> call, Throwable t) {
                    cFunction.loadingEnd();
                    Log.e(TAG, "uploadCustomers onFailure", t);
                    Toast.makeText(getContext(), "uploadCustomers onFailure", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

고객정보에 있어 적재가 되지 않는 에러가 발생하면 에러코드 "99"를 받아오고 팝업을 띄워 에러메세지를 출력하게 만들었다.

 

 

+ Recent posts