NetBeans 6 delivers great updates to the Matisse GUI builder. Spend a few minutes with Roman Strobl and get an expert briefing on what's new and what has changed. (sponsored)
In this, the third and final installation of Andres' Introduction to Groovy series, you learn about how Groovy handles variable numbers of arguments, named parameters, currying, and more about Groovy operators. Including, some new operators.
Swing Fuse (actually just Fuse), is a framework designed to make it easier to create your own custom desktop components. In this article, Daniel Spiewak shows you how to get started and provides sample source code you can download.
Willam Louth shows how he uses JXInsight Probes to investigate probable performance issues with code bases that he is not familiar with. He also highlights possible pitfalls in creating a benchmark, as well as in the analysis of results.
Recently I had to write a Generic Mock DAO object that simulates behavior that of Hibernate. That is if you call makePersistent(T entity), then if the Id field of the entity hasn't been set the method should set it otherwise check if the entity already includes the id and update the entity stored in the mock "data storage". Ok, so I had some reflection code as follows:
Now what happens is my coworker used the Generic Mock DAO but the id property of his Entity had a private setter, now when he called the makePersistent method a NoSuchMethodException was swallowed leaving the coworker puzzled for short time why is the mock dao not working. Naturally I changed getMethod to getDeclaredMethod and added setAccessible(true); (to enable access to private properties) and it worked. More importantly I changed error handling code to as follows:
now should somebody want to use the mock dao with an Entity without the id property a RuntimeException will be thrown meaning, he will know very soon where is the problem. Having an entity without the id property is a programmers error therefore it is true that in production the NoErrorException will never be thrown by the code but it sure doesn't hurt to throw a RuntimeException to signify a programmers error.
Moral of the story: even if you are sure that some errors will not be thrown consider throwing them as RuntimeExcpetions it will pinpoint the problem in an instant.
To signal stuff where the exception would truly mean a coding error from which no recovery should be attempted, I've used
AssertionError
instead of
RuntimeException
, often with a comment such as:
try
{
b = new BufferedReader(
new InputStreamReader(myInputStream,"iso-8859-1"), 1000);
}
catch (UnsupportedEncodingException uee)
{
// can't happen - jvm spec ensures iso-8859-1 supported
thrownew AssertionError(uee);
}
Or:
catch (DataConfigurationException dce) {
// Dave assures me this can't happen in this case
thrownew AssertionError(dce);
}
Do you have catch (NullPointerException e) anywhere in your code ? I hope not. You will still get full stack trace from embedded exception - so there is no difference at all in that case.
Original post reflects the problem with checked exceptions - in most cases it is not possible to handle them in meaningful way and they require a lot of lines of code (hiding important part of the code in visual clutter) to 'ground' - be it logging, rethrowing as runtime exception or rethrowing as next layer checked exception.
I tend to catch exceptions locally - for example NumberFormatException (which is runtime btw) exactly around the place where I'm parsing particular value. In such case, I can provide correct default/display error to user/whatever. Few other exceptions (IOException/SQLException come to mind) are reasonably useful and still can be sometimes recovered. All the rest - for me it is either Exception for logging and then some version of RuntimeException to throw up, or even Throwable to protect the executor threads from bad code corrupting the loop.
One thing I would add to this - for
InvocationTargetException
, you should really be throwing simply
e.getCause()
rather than wrapping it in something else.
The users of this wrapped object expect it to behave just like the real thing, right? In that case, it really should throw a HibernateException when the underlying thing throws a HibernateException.
The easy way to do this is to create your mock objects using Proxy.newProxyInstance(), whose invoke() method lets you throw an arbitrary Throwable. Throw whatever kind of runtime exception you want for other things (SecurityException, NoSuchMethodException, etc.), but if the invoker expected your mockFoo to be a real Foo, then you should be rethrowing .getCause() on any InvocationTargetException.
I like you web side; It is very interesting, important and structured well.
Many congratulations.
I invite you to investigate a little about my Web site and if you like to know but about my Web site they can visit it with much pleasure.
Costa Rica Real Estate
Come and enjoy the good life, Pura Vida!, as the locals say. The house and retirement of your dreams is still possible here as most real estate is still a bargin compared to the prices in the US and Europe. Own property with security in this democratic and politically stable country. Whether in the balmy highlands around San Jose, or the more tropical lowlands of the sea coasts on the beach, you will find the "finca", condo or home of your dreams. Peruse through the commercial and business opportunities below, from industrial properties to a hotel / marina for sale. Choose your residential parcel, lot, or house in the mountains or on one of the many beaches of Costa Rica. Or, enlist the aid of a Real Estate Agent that is familiar with the areas you wish to see.
For more information visit our web side http://www.costaricarealestate.com Thanks you.
I like you web side; It is very interesting, important and structured well.
Many congratulations.
I invite you to investigate a little about my Web site and if you like to know but about my Web site they can visit it with much pleasure.
Costa Rica Real Estate
Come and enjoy the good life, Pura Vida!, as the locals say. The house and retirement of your dreams is still possible here as most real estate is still a bargin compared to the prices in the US and Europe. Own property with security in this democratic and politically stable country. Whether in the balmy highlands around San Jose, or the more tropical lowlands of the sea coasts on the beach, you will find the "finca", condo or home of your dreams. Peruse through the commercial and business opportunities below, from industrial properties to a hotel / marina for sale. Choose your residential parcel, lot, or house in the mountains or on one of the many beaches of Costa Rica. Or, enlist the aid of a Real Estate Agent that is familiar with the areas you wish to see.
For more information visit our web side http://www.costaricarealestate.com Thanks you.
Don't Swallow Exceptions, revisited
At 9:37 AM on Mar 11, 2007, Alen Vrecko wrote:
Fresh Jobs for Developers Post a job opportunity
try { Method getIdMethod = entity.getClass().getMethod("getId", new Class[] {}); ... Method setIdMethod = entity.getClass().getMethod( "setId", new Class[] { typeOfId }); } catch (SecurityException e) { } catch (NoSuchMethodException e) { } catch (IllegalArgumentException e) { } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { }Now what happens is my coworker used the Generic Mock DAO but the id property of his Entity had a private setter, now when he called the makePersistent method a NoSuchMethodException was swallowed leaving the coworker puzzled for short time why is the mock dao not working. Naturally I changed getMethod to getDeclaredMethod and added setAccessible(true); (to enable access to private properties) and it worked. More importantly I changed error handling code to as follows:
} catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } catch (IllegalArgumentException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); }now should somebody want to use the mock dao with an Entity without the id property a RuntimeException will be thrown meaning, he will know very soon where is the problem. Having an entity without the id property is a programmers error therefore it is true that in production the NoErrorException will never be thrown by the code but it sure doesn't hurt to throw a RuntimeException to signify a programmers error.
Moral of the story: even if you are sure that some errors will not be thrown consider throwing them as RuntimeExcpetions it will pinpoint the problem in an instant.
8 replies so far (
Post your own)
Re: Don't Swallow Exceptions, revisited
Error handling is an art form that maybe less than 5% of all developers know how to master. If not worse.Re: Don't Swallow Exceptions, revisited
To signal stuff where the exception would truly mean a coding error from which no recovery should be attempted, I've used AssertionError instead of RuntimeException , often with a comment such as:try { b = new BufferedReader( new InputStreamReader(myInputStream,"iso-8859-1"), 1000); } catch (UnsupportedEncodingException uee) { // can't happen - jvm spec ensures iso-8859-1 supported throw new AssertionError(uee); }Or:catch (DataConfigurationException dce) { // Dave assures me this can't happen in this case throw new AssertionError(dce); }Re: Don't Swallow Exceptions, revisited
Why dont you just catch Exception as a whole?Re: Don't Swallow Exceptions, revisited
Do you want to get the RuntimeException instead of the NullPointerException in case the entity is null?Re: Don't Swallow Exceptions, revisited
Well, does it make any real difference ?Do you have catch (NullPointerException e) anywhere in your code ? I hope not. You will still get full stack trace from embedded exception - so there is no difference at all in that case.
Original post reflects the problem with checked exceptions - in most cases it is not possible to handle them in meaningful way and they require a lot of lines of code (hiding important part of the code in visual clutter) to 'ground' - be it logging, rethrowing as runtime exception or rethrowing as next layer checked exception.
I tend to catch exceptions locally - for example NumberFormatException (which is runtime btw) exactly around the place where I'm parsing particular value. In such case, I can provide correct default/display error to user/whatever. Few other exceptions (IOException/SQLException come to mind) are reasonably useful and still can be sometimes recovered. All the rest - for me it is either Exception for logging and then some version of RuntimeException to throw up, or even Throwable to protect the executor threads from bad code corrupting the loop.
Re: Don't Swallow Exceptions, revisited
One thing I would add to this - for InvocationTargetException , you should really be throwing simply e.getCause() rather than wrapping it in something else.The users of this wrapped object expect it to behave just like the real thing, right? In that case, it really should throw a HibernateException when the underlying thing throws a HibernateException.
The easy way to do this is to create your mock objects using Proxy.newProxyInstance(), whose invoke() method lets you throw an arbitrary Throwable. Throw whatever kind of runtime exception you want for other things (SecurityException, NoSuchMethodException, etc.), but if the invoker expected your mockFoo to be a real Foo, then you should be rethrowing .getCause() on any InvocationTargetException.
costa rica real estate
I like you web side; It is very interesting, important and structured well.Many congratulations.
I invite you to investigate a little about my Web site and if you like to know but about my Web site they can visit it with much pleasure.
Costa Rica Real Estate
Come and enjoy the good life, Pura Vida!, as the locals say. The house and retirement of your dreams is still possible here as most real estate is still a bargin compared to the prices in the US and Europe. Own property with security in this democratic and politically stable country. Whether in the balmy highlands around San Jose, or the more tropical lowlands of the sea coasts on the beach, you will find the "finca", condo or home of your dreams. Peruse through the commercial and business opportunities below, from industrial properties to a hotel / marina for sale. Choose your residential parcel, lot, or house in the mountains or on one of the many beaches of Costa Rica. Or, enlist the aid of a Real Estate Agent that is familiar with the areas you wish to see.
For more information visit our web side http://www.costaricarealestate.com
Thanks you.
costa rica real estate
I like you web side; It is very interesting, important and structured well.Many congratulations.
I invite you to investigate a little about my Web site and if you like to know but about my Web site they can visit it with much pleasure.
Costa Rica Real Estate
Come and enjoy the good life, Pura Vida!, as the locals say. The house and retirement of your dreams is still possible here as most real estate is still a bargin compared to the prices in the US and Europe. Own property with security in this democratic and politically stable country. Whether in the balmy highlands around San Jose, or the more tropical lowlands of the sea coasts on the beach, you will find the "finca", condo or home of your dreams. Peruse through the commercial and business opportunities below, from industrial properties to a hotel / marina for sale. Choose your residential parcel, lot, or house in the mountains or on one of the many beaches of Costa Rica. Or, enlist the aid of a Real Estate Agent that is familiar with the areas you wish to see.
For more information visit our web side http://www.costaricarealestate.com
Thanks you.