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

Cluster Table

Cluster Table is the another type of table which is supported by Oracle. A cluster is a schema object that contains one or more tables that all have one or more columns in common. Rows of one or more tables that share the same value in these common columns are physically stored together within the database. A cluster is to store data for more than 1 table in the same block. Tables which are created upon this cluster are called as “Cluster Table”. Cluster Index is an index which is created for this cluster. HOW TO VERIFY: How to verify whether the created table is “Cluster Table” or not? Fire this query, Select distinct cluster_name from user_all_tables where table_name IN ('EMPLOYEE',’DEPARTAMENT’) ð   This query would return the name of the cluster (eg., query returns “CLUSTER_DEPT_ID” if it is the created cluster’s name) LITTLE-KNOWN FACTS TO BE REMEMBERED: ·          Clustered Index doesn’t occupy any memory space though it is creat...

FBI – Function Based Index (for user defined functions)

FBI is one of the index types that is supported by oracle. It more or less behaves like other type of b*tree indexes(unique,non-unique,reverse) but only difference is the way it builds the index table. In this, when it builds Index table, index value is derived by applying the user defined function on the table data. So, index data value and actual table data value would be different here. FBI is simply an index that uses the function so that the database can make direct comparisons between the index values and the filter values. This FBI index can be created on a single column or more than one column. More than one FBI can be created for the same data table but for the different columns. HOW TO VERIFY: How to verify whether the created index is FBI or not? Fire this query, select index_type from user_indexes where index_name = 'TOTAL_SAL_INDX' ð   This query would return “ FUNCTION-BASED NORMAL ” (it means it is B*Tree index but of “FBI” type) LITTLE-KNOWN FACTS TO BE REM...

Hindi Joke: Rahman in Saudi

रहमान सऊदी अरब में  रहता था।  उसके साथ उसकी अम्मी  और अब्बा भी रहा करते  थे। अचानक उसकी माँ चल बसी तो उसने अम्मी का पार्थिव शरीर बक्से में पैक करवा कर अपने गाँव भेजा। गाँव में उसके भाई मकसुद ने बक्सा खोला तो देखा कि बक्से में अम्मी की लाश तो थी, लेकिन एक इंच जगह भी खाली नहीं थी। अम्मी के हाथ छाती पर थे और अंगुलियों में एक चिट्ठी फँसी थी। मकसुद ने ऊँची आवाज़ में वह चिट्ठी पढ़नी शुरू की। "प्यारे भाई मकसुद,शाहिद,शाह रुख,फैजल फिरोज और गफ्फुर , माफ़ करना मैं ख़ुद नहीं आ  सका क्योंकि मेरी तनख़्वाह काट दी जाती। मैं अम्मी को बक्से में इसलिए भेज रहा हूँ, क्योंकि वह चाहती थी कि उसका क्रियाकर्म गाँव में ही हो। लाश के नीचे इंपोर्टेड चॉकलेट के कई पैकेट रखे हैं, इसे बच्चों में बाँट देना। यहाँ अखरोट अच्छे मिलते हैं इसलिए तुम्हें बक्से में अखरोट के भी दो बड़े पैकेट मिलेंगे। अम्मी के पैरों में तुम्हें दो जोड़े सैंडल के और एक जोड़ा...