NHibernate Exception Handling

477 views
Skip to first unread message

CassioT

unread,
Oct 21, 2008, 10:12:35 AM10/21/08
to nhusers
Hi,

I was reading an article -

http://today.java.net/pub/a/today/2006/04/06/exception-handling-antipatterns.html

about exception handling and this paragraph took my attention:

"Wrapping an exception can provide extra information to the user by
adding your own message (as in the example above), while still
preserving the stack trace and message of the original exception. It
also allows you to hide the implementation details of your code, which
is the most important reason to wrap exceptions. For instance, look at
the Hibernate API. Even though Hibernate makes extensive use of JDBC
in its implementation, and most of the operations that it performs can
throw SQLException, Hibernate does not expose SQLException anywhere in
its API. Instead, it wraps these exceptions inside of various
subclasses of HibernateException. Using the approach allows you to
change the underlying implementation of your module without modifying
its public API."

I can catch MySql exceptions from nhibernate like this one: "Cannot
delete or update a parent row" (I know this is only the message but
the exception type is MySql.Data.MySqlClient.MySqlException)

Is this intentional or it will be modified in the future? What do you
think about it?

Thanks,

Cassio Tavares

Ayende Rahien

unread,
Oct 21, 2008, 10:18:48 AM10/21/08
to nhu...@googlegroups.com
That is likely to change (I think it has already been changed, for that matter).
NH now throws Hibernate exception only

Fabio Maulo

unread,
Oct 21, 2008, 10:25:00 AM10/21/08
to nhu...@googlegroups.com

CassioT

unread,
Oct 21, 2008, 12:30:47 PM10/21/08
to nhusers
I'm using version 2.0.1.4000

Should ISQLExceptionConverter be used by the final developer or inside
NHibernate
code? In other words, we're gonna catch a database exception and
convert it or NHibernate
will do this for us?

Thank you

Cassio Tavares

On 21 out, 12:25, "Fabio Maulo" <fabioma...@gmail.com> wrote:
> Which version of NH are you using ?
> To catch and convert your RDBMS exception the right way is an implementation
> of
> ISQLExceptionConverter
>
> BTWhttp://groups.google.com.ar/group/nhusers/browse_thread/thread/c42e47...
>
> http://groups.google.com.ar/group/nhusers/browse_thread/thread/3f2b98...
>
> http://groups.google.com.ar/group/nhusers/browse_thread/thread/72c1e8...
>
> 2008/10/21 CassioT <cass...@gmail.com>
>
>
>
>
>
> > Hi,
>
> > I was reading an article -
>
> >http://today.java.net/pub/a/today/2006/04/06/exception-handling-antip...

Fabio Maulo

unread,
Oct 21, 2008, 1:10:36 PM10/21/08
to nhu...@googlegroups.com
Please use the previous post visiting the links and take a look to the example I had mentioned there.
NH catch exception and use the ISQLExceptionConverter where available to re-throw the exception this mean that you can manage exceptions as you prefer.

2008/10/21 CassioT <cas...@gmail.com>



--
Fabio Maulo

CassioT

unread,
Oct 21, 2008, 2:06:01 PM10/21/08
to nhusers
I've visited the links and I can tell you that this is exactly what I
was
looking for. I didn't know that was a Jira issue opened for that and
I
started to make something very similar in the project that I work.

if (ex is MySqlException)
{
MySqlException mex = ex as MySqlException;

switch (mex.Number)
{
case (int)MySQLError.DeleteParentRow:
ex = new System.Data.DataException("...");
break;

case (int)MySQLError.DuplicateKey:
ex = new System.Data.DataException("...");
break;
}
}

public enum MySQLError : int
{
[Description("Cannot delete or update a parent row: a foreign key
constraint fails (%s)")]
DeleteParentRow = 1451,

[Description("Can't write; duplicate key in table '%s'")]
DuplicateKey = 1022
}

I'll take a closer look to that Jira issue later.

Thank you,

Cassio Tavares

On 21 out, 15:10, "Fabio Maulo" <fabioma...@gmail.com> wrote:
> Please use the previous post visiting the links and take a look to the
> example I had mentioned there.NH catch exception and use the
> ISQLExceptionConverter where available to re-throw the exception this mean
> that you can manage exceptions as you prefer.
>
> 2008/10/21 CassioT <cass...@gmail.com>
Reply all
Reply to author
Forward
0 new messages