aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Clarification about Exception handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Clarification about Exception handling" Watch "Clarification about Exception handling" New topic
Author

Clarification about Exception handling

Garann Means
Ranch Hand

Joined: Jan 28, 2002
Posts: 214
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

Joined: Jul 22, 2000
Posts: 9052
    
  12
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.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
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!


Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
Garann Means
Ranch Hand

Joined: Jan 28, 2002
Posts: 214
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

Joined: Jul 03, 2001
Posts: 2120
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.


SCJP2. Please Indent your code using UBB Code
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Garann Means
Ranch Hand

Joined: Jan 28, 2002
Posts: 214
That helps a lot. Thanks, Jose and Corey.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Clarification about Exception handling