• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to catch an exception thrown from a message invoked during instance variable initialization ?

 
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

Currently in our system, we have hardcoded a String (tokens) to a static final instance variable and a new solution is proposed to read the string from a properties file instead.

Since our class would be called by sessionbean for every incoming request, we do not want to have the properties file reading functionality to be called for every request and hence I thought of writing a static method which reads the file for the first time the class is loaded. Something like below



Now the problem is readFile() method can throw an Exception, if it could not read the file or for something else and I can not catch the exception and swallow it. I need to throw the exception. But how can this be handled at the invocation at private static final String str=readFile()

The other option, I have is to modify as



Is the second approach a better one than the first ? I'm inclining on the first approach, if I could find a way to tap the exception, as I'm doing what ever initiation to be done during class loading phase.

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if readFile() fails , you should stop the process and need to show error message to a user.so Exception terminate your process either case, so you have to concentrate on redirecting the message to the user
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you could use a static initializer block:
 
Kumar Raja
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:if readFile() fails , you should stop the process and need to show error message to a user.so Exception terminate your process either case, so you have to concentrate on redirecting the message to the user



That is exactly what I'm looking for. But how? We are invoking this method during instance variable initialization.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kumar Raja wrote:But how? We are invoking this method during instance variable initialization.



You can throw RuntimeException instead of Checked Exception or use try/catch
 
Kumar Raja
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wouter Oet wrote:Or you could use a static initializer block:



Thank you Wouter. But again, my problem here is, I do not want to swallow the exception but rather throw it out. I'm thinking that my option 2 is a better one. Does any one have any suggestion on this ?
 
Ranch Hand
Posts: 152
VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why do you want to notify the user?

If tyhis were my design, I would create a sigleton
for that application in an Application Lifecycle Listener.

Straight forward, full exception handling etc.

You could configure your application
to not go into RUNNING state if something
went wrong when creating the singleton.

Just an opinion.
Matt
 
reply
    Bookmark Topic Watch Topic
  • New Topic