• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Throw a "Throwable" object from a static block checking if the DB driver is loaded

 
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a static block which is checking if the DB2 driver is loaded using a try catch block. If it not loaded, I am trying to throw the Throwable object, but getting a compile time error, suggesting to surround this throw statement with another try catch block. Please suggest if I can do this in any other way?

 
Ranch Hand
Posts: 115
11
IntelliJ IDE Clojure Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should be able to propagate the exception if you wrap it in as a RuntimeException. A static block cannot throw any exception that is not a RuntimeException.



Something about this approach seems a little weird to me, though...
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

swathi bairu wrote:I have a static block which is checking if the DB2 driver is loaded using a try catch block. If it not loaded, I am trying to throw the Throwable object...


Any particular reason why?

The method already throws three perfectly good exceptions, so I'm wondering why you think you benefit from gathering them all under one "umbrella".

There may, of course, be a perfectly good explanation, but without one, it just looks like "noise" code.

Winston
 
Saloon Keeper
Posts: 26025
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is, where do you expect to throw the exception to?

For instance methods (including constructors), there's application code somewhere that the thrown exception can percolate back up to. For static construction methods, the only place that the exception could go to would be to the classloader, which generally means all the way up and out of the JVM.

If I'm not mistaken, both Throwable and RuntimeException are unchecked exceptions - Throwable being the parent of all exceptions. An actual checked Exception would be a problem, since as far as I know, you cannot put a "throws" qualification on a code block.

Realistically, what would probably be a better solution would be to use a factory method when you want to obtain the database connection and defer the actual driver load until the first time the factory method is called. Let the factory method deal with the exception.

And, of course, if you're coding a web application, you shouldn't be doing the driver load in application code at all. Use a Connection Pool.
 
Marshal
Posts: 76440
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . Throwable and RuntimeException are unchecked exceptions . . .

No, Throwable is checked.

The API for Throwable wrote:. . . For the purposes of compile-time checking of exceptions, Throwable and any subclass of Throwable that is not also a subclass of either RuntimeException or Error are regarded as checked exceptions. . . .

 
swathi bairu
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:
Any particular reason why?


This class gets called by a shell script in a batch environment. So for logging purposes, I want to throw this Throwable object.
 
Campbell Ritchie
Marshal
Posts: 76440
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

swathi bairu wrote:. . . This class gets called by a shell script in a batch environment. . . .

Please show us that is good practice.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

swathi bairu wrote:This class gets called by a shell script in a batch environment. So for logging purposes, I want to throw this Throwable object.


OK, so presumably you create your own class that extends Throwable, and then throw it wrapping the original exception.

I can see how that might help a search algorithm for your log (though only marginally), but only at the expense of adding complexity at both ends of the process.

My advice: This sounds like "noise" code to me; so don't do anything until you can explain to US (or your boss) why you think you need to. Or unless you've been TOLD to do it - and even then, it might be worth raising a query.

Winston
 
swathi bairu
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:
This sounds like "noise" code to me; so don't do anything until you can explain to US (or your boss) why you think you need to. Or unless you've been TOLD to do it - and even then, it might be worth raising a query.



I am not sure if this is a good practice. But this is how I was told to do by my lead.
 
Campbell Ritchie
Marshal
Posts: 76440
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A little potassium cyanide in the boss' tea will sort out all your programming problems
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

swathi bairu wrote:I am not sure if this is a good practice. But this is how I was told to do by my lead.


OK, well if you don't think you can adopt Campbell's solution, this is how I'd do it:

1. Create my own class that extends Error (which by definition also extends Throwable) - DriverInitializationError?

2. Create a method that obtains the driver, in a utility class if necessary, viz:
3. Use it to initialize my driver, viz:rather than adding a static block.

Why? Because if I need to do this in 7 different places, I only have to change the code in ONE place if it needs modification.

That said, this way of initializing a driver has been superceded since 1.4 by the DataSource pattern (which you can read about here), so you might want to ask your boss if you can use that rather than faffing around with Drivers.

HIH

Winston
 
swathi bairu
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Winston. That kind of gave me an idea of how could we do it.
 
This is my favorite tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic