This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

declare or handle

 
Jo Lee
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help me in clarify this about Exception handling. If one declare an exception, does one still need to do a try /catch in the methods? or is it legal to do a try/catch block in a method that has declared an exception?
void method_1()throws IOException
{
try{
something
}catch(EOFException eofe){
something
}
}
is the above legal?
Thanks for any help
 
Jo Lee
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another question is : is the following also legal? please explain why?
void method_1() throws IOException
{
method_2()
}
void method_2() throws IOException
{
try{
method_3();
}
catch(EOFException eofe){
...
throw eofe
}
}
void method_3() throws IOException
{
...
}
Thanks for any help
 
Jo Lee
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A third quesiton about this: is the following legal?
double quto(int i, int j)
{
if(j==0) throw new DividByZeroException();
else
return (double)i/j;
}
thanks again
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jo Lee:
If one declare an exception, does one still need to do a try /catch in the methods? or is it legal to do a try/catch block in a method that has declared an exception?

is the above legal?

Yup. That's fine. Just try compiling it to see. It is perfectly legal to handle exceptions within a method that states that it might throw an exception.
Corey
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jo Lee:
Another question is : is the following also legal? please explain why?


Sure, this is also fine. You can throw another exception from within a catch or finally block. One word or warning, however: if you throw an exception within a try or a catch block and you have a finally block that also throws an exception, the exception that was thrown from the try or catch block will be ignored with respect to the one thrown by the finally block.
Corey
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jo Lee:
A third quesiton about this: is the following legal?

thanks again

You don't need to declare an exception is thrown as long as that exception is a Runtime Exception. If DivideByZeroException extends RuntimeException, this is fine. If it extends Exception, however, you'd be required to declare that the method throws such an exception.
Just as a sidenote - if you try to divide by 0 in Java, an ArithmeticException (a descendent of RuntimeException) is thrown).
Corey
 
Jo Lee
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corey;
thanks for the clarification. a question about the throw eofe in the catch block, the eofe is throw back to the caller, right?
if the try/catch block is in a for loop, would rethrow eofe in the catch block allows the for loop to go on to the next iteration? would it make any difference, if the for loop is an endless loop?
in a endless for loop, if an exception is caught, would the loop been ended, or do we need to use additional code such as break in the catch block to get out of the for loop.
thanks again for your reply
Jo
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jo Lee:

the eofe is throw back to the caller, right?

All thrown exceptions are always throws to the method that invoked the method that threw the exception.

if the try/catch block is in a for loop, would rethrow eofe in the catch block allows the for loop to go on to the next iteration? would it make any difference, if the for loop is an endless loop?
in a endless for loop, if an exception is caught, would the loop been ended, or do we need to use additional code such as break in the catch block to get out of the for loop.

If an exception is thrown, even from within a loop, the execution of that block ends abruptly and the exception is either handled by an appropriate try/catch block or thrown to the invoking method.
I hope that helps,
Corey
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The intent of being able to declare exceptions that are not thrown is to specify the maximum exceptions that the overriding methods are allowed to throw. That is, declaring a checked exception in a method prevents the overriding methods from declaring checked exceptions that were not declared in the superclass method, or their supertypes.
Useful, for example if the method is abstract.
[ June 04, 2002: Message edited by: Jose Botella ]
 
Jo Lee
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corey;
Thanks again. The reason that I ask about that is that we have the following notes:
Corey
Thanks again.
The reason that I am asking about all those is that our teacher has the following slides:
example 1:
try
{
for(;
{
some action()
}
}
catch (EOFException e)
{
System.our.println()
}
System.out.println("control passed to here")
The explanantion that the teacher gives is that:
when the user input ^Z or ^D, it signals end of input and EOFException is generated. The exception is handled by the catch block and control then is passed to the last System.out.println line.
It is ok so far, but then in the example 2:
someAction() in examples 1 does not handle NumberformatException, and would result in program termination if it arose. What we should do is inform the user, but in order to continue reading the input stream, we have to nest the try and catch blocks inside the endless loop.
for(;
{
try{
someAction()
}catch{NumberFormatException e){
printout something
}
}
System.out.println("average =" +(double)total/number);
In this case, if it is true, after the exception NumberFormatException is caught, the control is returned to the endless for loop and we can have the next round of reading input again, right?

Example 3 is the same as above, only that two catch block is provided, says after EOFException is caught, the control is passed back to endless for loop , which is not what we want, therefore, a break is introduced:

for(;
{
try
{someAction()
}catch(EOFException e){
printout something
}
catch(NumberFormatException e){
printout something again
}
break;
}
System.out.println("something")

All the above is where my confusion is from.
From the above, my conclusion is that once an exception is caught by a catch block, the code control is returned to the method that calls the try/catch block and if that method is an endless for loop, the for loop will go on the its' next iteration unless some action is taking to break out the for loop, such as either a break right after the try/catch block or in the catch block, or an exception happens that was not caught.
Pleas help with me on if it is right?
Thanks
Jo
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Jo, that's right. The exeuction path, when dealing with try/catch blocks goes as such:
1. Execute code within try block.
2. If no exception is thrown, execution goes to finally block (Skip to Step 5).
3. If an exception is thrown, execution goes to first applicable catch block.
4. If no applicable catch block is found, execution goes to the finally block.
5. If no finally block is found, one of two cases arise:
5a. If an exception went unhandled, it is thrown to the invoking method.
5b. If all exceptions were handled, execution resumes following the finally block.
Therefore, as the exception is handled within the for loop, execution resumes within the for loop and continues looping.
Check out the JLS, §11.3 Handling of an Exception for more information.
I hope that helps,
Corey
 
Jo Lee
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corey;
Thanks very much for the help. It is much more clear now. Also thanks for the link to JLS's chapter.
Jo
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic