File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Try/Catch question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Try/Catch question" Watch "Try/Catch question" New topic
Author

Try/Catch question

Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
Ok say i have the following:



First the reason I am wanting to throw the exception in both the executeSprocInsert and createItem function is because
just system.out.println the message in the createItem method wouldn't work in web apps, so i was just gonna throw an
exception so i could handle it any way i wanted in any app.

Is this a good or bad design, if bad, how could I improve the structure?

Thanks,

Justin


You down with OOP? Yeah you know me!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60049
    
  65

What is the point of catching an exception only to rethrow the same exception? Why not just let the original exception propagate outward?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2771
    
  10

I generally don't like catching one exception and throwing another one. It can make debugging harder, because if you get a stack trace, you can only track the problem back to where you threw the new exception. Sometimes you have no choice, because you want to throw a different exception, or add information or something like that. I don't see you doing that here, so what's the purpose of catching and rethrowing? In any case, you can wrap one exception in another, which can make debugging later so much easier!
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
Oh ok, so should i just take out the throws sqlexception and the try/catch in the executeSprocInsert, remove the try/catch in createItem() and just try/catch in the main?

Thanks,

Justin
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60049
    
  65

If you have nothing to do, it's useless -- in fact, disruptive -- to catch the exception only to rethrow it.

Since you mentioned web apps, I'll focus on those.

Pretty much the only times you should catch an exception are:

1) When you want to stop it and take some other action. For example, catching a NumberFormatException when parsing an integer to provide a default or report the error to the user in a form other than an exception. (Exceptions should never be used to report data entry errors).

2) When you want to convert one exception to another type. For example to "convert" an exception of one type to a ServletException because that's all that's allowed to propagate outward. In this case, the information in the original exception should not be lost, so attach it to the new exception as its cause.

Otherwise, you should just let the exceptions propagate outward uninterrupted so that whatever mechanism you have declared in the deployment descriptor can handle all such exceptions in a common and deterministic manner.
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
Cool, alright..

But what if someone is inserting/Updating inventory into the database from front end app, And the modelnumber is the primary key,
and they enter the same model number, shouldn't I catch the sqlexception and let them know that they can't insert duplicates?

Or should I query the db before I call the insert to see if the modelnumber they entered is already there? And would either approach
be substantially more effecient than the other?

I'm not trying to challenge the idea that catching one exception and throwing that same one is dumb, but I just need to know how
to redo the code so it will be very re-usable and some-what efficient.

Consider this code for my question.



Thanks for all the info,

Justin
Marty Fried
Greenhorn

Joined: Feb 10, 2009
Posts: 18
Justin Fox wrote:Cool, alright..

But what if someone is inserting/Updating inventory into the database from front end app, And the modelnumber is the primary key, and they enter the same model number, shouldn't I catch the sqlexception and let them know that they can't insert duplicates?
. . .
Justin

I think the exception should be handled by the code that can correct the situation in an intelligent manner. I suspect that the suggestion for number 1 from Bear might mean that you shouldn't use an exception to report data entry errors might be referring to reporting it in lower-levels that don't have the context to do something about it, but that's just a guess - I might be missing something.

I'm not a Java expert, but I've had lots of C++ experience, and that's what I'm going on here.

-- Marty Fried
Left Coast, USA
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60049
    
  65

Yes, Marty has it right -- that's exactly the scenario of #1. You catch the exception to do something else with it -- in this case, report the error to the user in a reasonable manner. Showing them a stack trace is not a "reasonable manner".

In no way is catching an exception simply to throw it again a useful exercise.
Marty Fried
Greenhorn

Joined: Feb 10, 2009
Posts: 18
Bear Bibeault wrote:Yes, Marty has it right -- that's exactly the scenario of #1. You catch the exception to do something else with it -- in this case, report the error to the user in a reasonable manner. Showing them a stack trace is not a "reasonable manner".

In no way is catching an exception simply to throw it again a useful exercise.

Oooh, my first post, and I didn't make a fool of myself!

But I should have proofed it better, since I noticed now that it's not worded well. Also, I should have made it clear that I thought he was doing the right thing. I have this bad habit of rushing to finish before someone gets in an answer that makes me look like I'm repeating their post.

Anyway, thanks for clearing it up.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60049
    
  65

Welcome to the Ranch, Marty!
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
Cool, got it now.

Thank,


Justin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Try/Catch question
 
Similar Threads
Exceptions in Overriding methods??
Exceptions and interfaces
Indicate non-fatal error without Exception
wizlab mock question
Could not locate DB driver