This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clarification about Exception handling

 
Garann Means
Ranch Hand
Posts: 214
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I just tried to explain this to a co-worker, and discovered that I'm still confused about how a checked Exception has to be handled.
Say ClassA contains the line:
int someResult = ClassB().getMyDatabaseConnection();
The method getMyDatabaseConnection() contains code that could cause a SQLException, so, if I'm correct, getMyDatabaseConnection() must either place this potentially dangerous code in a try/catch block or declare that it throws an Exception.. Right?
And if getMyDatabaseConnection() chooses to throw the Exception, ClassA must be ready to handle it by putting its call to the method in a try/catch block.. Right?
What, then, does ClassB have to do? Suppose that ClassB's constructor calls the method, or its main method calls it, or it doesn't actually get called within ClassB anywhere but is instead a "getter" for other classes to use - does ClassB ever throw the Exception? If so, in which case(s)? I'm 99% sure that the class doesn't ever throw an Exception.. Is that right?
Thanks for any clarification,
g.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say ClassA contains the line:
int someResult = ClassB().getMyDatabaseConnection();

The method getMyDatabaseConnection() contains code that could cause a SQLException, so, if I'm correct, getMyDatabaseConnection() must either place this potentially dangerous code in a try/catch block or declare that it throws an Exception.. Right?


Right

And if getMyDatabaseConnection() chooses to throw the Exception, ClassA must be ready to handle it by putting its call to the method in a try/catch block.. Right?

Actually, any method that calls the getMyDatabaseConnection() method (not ClassA or ClassB) must be ready to handle it by putting its call to the method in a try/catch block or declaring it.

What, then, does ClassB have to do? Suppose that ClassB's constructor calls the method, or its main method calls it, or it doesn't actually get called within ClassB anywhere but is instead a "getter" for other classes to use - does ClassB ever throw the Exception? If so, in which case(s)? I'm 99% sure that the class doesn't ever throw an Exception.. Is that right?

ClassB doesn't have to do anything. Classes don't throw, catch or declare exceptions; methods throw, catch and declare exceptions. If main() calls the getMyDatabaseConnection() method, it will have to deal with either catching or declaring the checked exception that the getMyDatabaseConnection() method declared.
 
Younes Essouabni
Ranch Hand
Posts: 479
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
correct me if I'm wrong.
Every code that may throw an exception must be in a try block.
1�)If the method catch the exception, others method calling the "dangerous" method, don't have to care about the exception, since it's already caught.
2�)If the method (containing the code) doesn't catch the exception, then the method must declare the exception. And every method calling the "dangerous" method must declare or catch the exception.
I hope I was clear enough!
 
Garann Means
Ranch Hand
Posts: 214
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both!
One more question..
If I have this code:

..and getCurrentThing() throws a checked Exception, would I change the code to this?

g.
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried to compile it?
Because try and catch are in no method it won't.
In the first example, this, from JLS 8.6 is useful:

An instance initializer of a named class may not throw a checked exception unless that exception or one of its superclasses is explicitly declared in the throws clause of each constructor of its class and the class has at least one explicitly declared constructor.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. That's not legal syntax. You are not allowed to invoke a method is an assignment initialization that may throw an exception. If you were to try, the compiler would issue an error. Also, you can't catch it, as you've tried because you'd only confuse the compiler as it can't execute a try/catch block outside of a valid block. Rather, I think you'd rather use an instance initializer, like this:

I hope that helps,
Corey
 
Garann Means
Ranch Hand
Posts: 214
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That helps a lot. Thanks, Jose and Corey.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic