-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 
Author Message
 Post subject: HibernateUtil - catch Throwable
PostPosted: Mon Sep 25, 2006 11:33 pm 
Newbie

Joined: Mon Sep 25, 2006 4:54 pm
Posts: 5
I am using the HibernateUtil class that comes with Hibernate install (this utility class is also mentioned in the HIA book). The static initializer block in the class has a catch block for Throwable. I am using this class in my project but I can't reproduce a scenario where Throwable (instead of HibernateException or Exception) is thrown in the code. There is a comment in this code block saying that "We have to catch Throwable, otherwise we will miss NoClassDefFoundError and other subclasses of Error". I have tried to rename the hbm mapping xml file (to some invalid name), mapping java class name, and even the SQL table name (to an invalid table name) but still it just throws HibernateException and not Throwable.

Has any one run into a situation where they had to catch Throwable object (instead of HibernateException)? If so, can you please reply to this post on what could cause a problem when initializing Hibernate Configuration stuff where we need to catch Throwable.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 2:17 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Uhm, Throwable is an interface. You will never see a "thrown" Throwable. What you will see "thrown" is Error and Exception, which implement Throwable. Read the JDK documentation about Error and Exception.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 2:19 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
And you would be the first beginner programmer who could _not_ manage to produce a NoClassDefFoundError :) Just remove a required JAR file from your classpath.


Top
 Profile  
 
 Post subject: HibernteUtil question
PostPosted: Tue Sep 26, 2006 6:59 pm 
Newbie

Joined: Mon Sep 25, 2006 4:54 pm
Posts: 5
Thanks Christian. One of the best practices we are following is not to catch Throwable or even Exception in the java classes. What's your suggestion on just catching HibernateExcetion in the static init block? If there are any mapping class related issues, they should be caught in the developer local environment. By the way, isn't Throwable a class? (http://java.sun.com/j2se/1.4.2/docs/api ... wable.html). But I see your point of not handling at Throwable but its subclasses.


Top
 Profile  
 
 Post subject: HibernateUtil - catch Throwable
PostPosted: Wed Sep 27, 2006 12:45 pm 
Newbie

Joined: Mon Sep 25, 2006 4:54 pm
Posts: 5
No matter what exception is thrown in the static init block, it will get wrapped in an ExceptionInInitializerError. For example, the sample code below throws a RuntimeException which is in turn wrapped in an ExceptionInInitializerError.

final class ErrorTest {

static {

if (true) {
throw new RuntimeException("Foo");
}
}

public static final void main(final String[] args) {}
}

So why catch Throwable and then wrap it manually in an ExceptionInInitializer error when the JVM will do it for you?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 27, 2006 1:32 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Because I've made the experience that this is not the case in some environments and with some errors/exceptions. You are welcome to leave it out. I won't remove it from the examples though. Happy?


Top
 Profile  
 
 Post subject: Throwable is not an interface
PostPosted: Wed Sep 27, 2006 1:38 pm 
Newbie

Joined: Wed Sep 27, 2006 1:15 pm
Posts: 10
Despite its name Throwable is not an interface but class:

just check it here:

http://java.sun.com/j2se/1.5.0/docs/api/


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 27, 2006 1:41 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
A class has an interface. You can implement an interface. Error and Exception do, by subclassing. Can we close this thread now?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 27, 2006 1:49 pm 
Newbie

Joined: Mon Sep 25, 2006 4:54 pm
Posts: 5
Christian, Thanks for your help. I will try to remove the catch Throwable and see if we run into issues with that approach. You may close this thread.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 27, 2006 2:32 pm 
Newbie

Joined: Wed Sep 27, 2006 1:15 pm
Posts: 10
Generally speaking this is not a Hibernate question, but just in order to clear the confusion: Throwable is a base class to Exception (which in turn fall into two categories - checked exception and unchecked exceptions) and Error.

When you catch an exception you specify exception calss name. However, if for some reason unchecked exception was thrown, you may need to catch it as well.

Since unchecked exceptino not specified in 'throw' list, most common approach is to catch Throwable since Throwable is a base class. The disadvantage of this approach is Errors are caught as well, and if you catch an Error, you are supposed to know how to handle it.

Throwable itself (again, it is real class) is not usually thrown.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 27, 2006 3:41 pm 
Newbie

Joined: Mon Sep 25, 2006 4:54 pm
Posts: 5
well, the original question was why do we need to catch Throwable in the static initializer block in HibernateUtil class. The discussion got kinda side tracked to the explanation on Throwable and its subclasses. I appreciate all the responses as they really helped me understand the design behind HibernateUtil.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.