Skip to main content

Error messages while throwing exceptions

I was very concerned about exceptions, how they are thrown and handled. I define a proper error code before throwing some error. I never been to a production site, but this time I got a chance to be a implementation team. The way I handled my exceptions seems to be incomplete. Though, my error messages in exception was good, but it still missing a feature, which could help production people. Many of you might not aware of the very fact that, the logging level is severe in production. Then way you log the data in the codes is always helpful, but try to handle your error message for exceptions efficiently that you need not to enable logging to finer levels to analyze the issue. The errors must be self explanatory.

Analyze the code snippet mentioned below:

public void fetchCurrencyOffset(String bankCode, String branchCode, String currencyCode1, String currencyCode2) {
            try {
                  Map<String, Object> objectMap = new HashMap<String, Object>();
                  objectMap.put(CommonServicesConstants.CURRENCY_CODE_ONE, currencyCode1);
                  objectMap.put(CommonServicesConstants.CURRENCY_CODE_TWO, currencyCode2);
                  objectMap.put(CommonServicesConstants.BRANCH_CODE, branchCode);
                  objectMap.put(CommonServicesConstants.BANK_CODE, bankCode);
                  executeNamedQuery(CommonServicesQueryConstants.QUERY_TOGET_CCY_OFFSETFLAG, objectMap);
            } catch (BusinessException e) {
                  throw new BusinessException(CommonServicesErrorConstants.FAILED_LOAD_CURRENCY_PAIR);
            }
      }
      private void executeNamedQuery(String queryName, Map<String, Object> objectMap) {
            // Fire DataBase query.
      }  


It seems perfectly right, I don’t see any error in the above code snippet. Whenever, something breaks here in the above code snippet, it will surely, throw a relevant Business Exception. So, check the exception stack trace, how it looks like.

[2011-07-11T19:43:00.097+10:00] [host_server1] [ERROR] [] [com.test.practice.CurrencyService] [tid: [ACTIVE].ExecuteThread: '14' for queue: 'weblogic.kernel.Default (self-tuning)'] [userId: xxxxxx] [ecid: e9e4f880efb8a51d:-28bac7cc:1310f48109c:-8000-00000000000090af,0] [APP: xxxxxxx-middleware] XX_CS_CY_1015[[
com.test.practice.framework.exception.BusinessException: Failed to load Currency Pair
at com.test.practice.commonservice.currency.CurrencyService.fetchCurrencyOffset(CurrencyService.java:3891)
at com.test.practice.commonservice.currency.CurrencyService.fetchSpotRate(CurrencyService.java:1481)

Looking at the above stack trace, one can easily identify the error. So, I was in the dilemma that this is a good error message for exceptional handling practice. But I encountered this error in the production site also. When I was asked to look into the issue, I analyzed this issue and reported that Currency pair was missing from the database. When my supervisor asked me that which currency pair was missing?..I was like, I don’t have any answer, I asked him to enable the log level to fine level to check that. So, the problem was solved at that time. This is the point when I realized the importance of error messages of exception handling. Thus. I started looking for some alternative ways for the same. And, finally I discovered this, analyze the same code snippet now :

public void fetchCurrencyOffset(String bankCode, String branchCode, String currencyCode1, String currencyCode2) {
            try {
                  Map<String, Object> objectMap = new HashMap<String, Object>();
                  objectMap.put(CommonServicesConstants.CURRENCY_CODE_ONE, currencyCode1);
                  objectMap.put(CommonServicesConstants.CURRENCY_CODE_TWO, currencyCode2);
                  objectMap.put(CommonServicesConstants.BRANCH_CODE, branchCode);
                  objectMap.put(CommonServicesConstants.BANK_CODE, bankCode);
                  executeNamedQuery(CommonServicesQueryConstants.QUERY_TOGET_CCY_OFFSETFLAG, objectMap);
            } catch (BusinessException e) {
                  throw new BusinessException(CommonServicesErrorConstants.FAILED_LOAD_CURRENCY_PAIR, currencyCode1, currencyCode2);
            }
      }
      private void executeNamedQuery(String queryName, Map<String, Object> objectMap) {
            // Fire DataBase query.
      }


The above snippet also looks the same except the line where business exception is thrown.
Now, when the same error comes again, the stack trace looks a bit different.

[2011-07-11T19:43:00.097+10:00] [host_server1] [ERROR] [] [com.test.practice.CurrencyService] [tid: [ACTIVE].ExecuteThread: '14' for queue: 'weblogic.kernel.Default (self-tuning)'] [userId: xxxxxx] [ecid: e9e4f880efb8a51d:-28bac7cc:1310f48109c:-8000-00000000000090af,0] [APP: xxxxxxx-middleware] XX_CS_CY_1015[[
com.test.practice.framework.exception.BusinessException: Failed to load Currency Pair with ccy1 : INR and ccy2 : AUD
at com.test.practice.commonservice.currency.CurrencyService.fetchCurrencyOffset(CurrencyService.java:3891)
at com.test.practice.commonservice.currency.CurrencyService.fetchSpotRate(CurrencyService.java:1481)

Just a small change is required in the error message resource bundle and I was done.

So, after looking this stack trace, I can easily figure out the missing pair of currencies. Thus, for any error message, we always pass some piece of information in the error messages so, we need not to enable the fine level of the logging every time.

Comments

Popular posts from this blog

Bitmap index

Bitmap index is one of the index types that is supported by oracle. Unlike B*tree index, this is very compressed index. It means if I create a bitmap index on a column, then the generated index table will be smaller when compared the index table which is generated by the binary index on the same column. The reason is, in bitmap index, ROWIDs won’t be stored in the index table, instead index values will be mapped against bitmaps. Bitmap is nothing but it stores either of this values, 1(Match) or 0(No Match). For each distinct values in the column, a separate index record will be created. Unlike b*tree index, bitmap index can accept NULL and it creates separate index entry for this. Bitmap index will work better if number of distinct values are relatively less when compared to the total number of records. Why is it referred as “relatively less”? this index will work fine if table of 1,000,000,000 records contains 5000 distinct values and if table of 1,000 records contains 5 distinct va...

Hash Natural Join

To perform hash join, Oracle follows these steps: 1.      Oracle chooses the smallest of two tables as the hash table (otherwise called as driving table). Oracle built the hash table in RAM after applying the hash function on the joining column(s) of the driving table. 2.      Oracle chooses the other [big] table as the probe table (otherwise called as driven table or probing table). It traverse through all the records of this probe table, applies the same hash function on the joining column(s) [column(s) used to join these two tables] and will hit the corresponding entry in the hash table. 3.      Oracle returns the output if a record from driving table is already present in the same hash key, else no record will be returned. It may look like Nested loop join & Hash join have the same architecture since both these have the concept of driving & driven tables but they have entirely different design if you closely look a...

Index Range Scan

Index Range Scan is one of the index access methods supported by Oracle. Index Range Scan means the retrieval of one or more ROWIDs from an index. Indexed values are generally scanned in ascending order. Index Range Scan is applicable to both B*Tree Unique Index and B*Tree Non-Unique Index unlike Index Unique Scan, where it is applicable only to B*Tree Unique Index. LITTLE-KNOWN FACTS TO BE REMEMBERED: ·          If Oracle has to follow Index Range Scan, if B*Tree Unique index is created on the table, then in the SQL, any non-equality operator must be used like <=, <, >, >=, IN, BETWEEN. If any of these operators is used, it means more than one index record is going to be referred in the index table and in turn which returns more than one ROWID (because in the unique index, single index value is mapped to single ROWID. So if 5 index records are accessed, it means 5 ROWIDs are retrieved). It is explained in EXAMPLE section. ·  ...