• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

A query on exception handling

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody,

Consider the following code-- (Taken from javabeat mock exam)

public class Test3{

public static void main(String args[]){
System.out.println(method());
}

public static int method(){
try{
throw new Exception();
}
catch(Exception e){
throw new Exception();
}
finally{
return 3;
}
}
}

In this case I am throwing a new exception in catch block but not catching it nor I am declaring that the method throws any exception.Java requires all checked exceptions be properly handled or declared.Otherwise compiler complains.

However in this case,the program compiles and runs successfully.It gives output 3.

But if I comment out finally block, compiler complains that the exception must be handled or declared.

That means finally block is causing the program to compile even if it is doing an illegal thing (throwing an exception that is not handled or declared). Is it not contrary to what we expect? (Compiler should never allow any illegal thing no matter what)?How does finally block make a difference?

Your response will be greatly appreciated.

Thanks

---Vishram
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Vishram

here is your code



here finally block makes a difference bcoz

your methods return type is an int
as below

public static int method(){

and hence your method must return
an integer..........
which is returned in finaly
block


as you are returning as integer
in the finally block hence here
without finally block your code is not
compiling

if you want to comment finally
just change the return type
of your method to void........
it will definately compile !!!

i hope it helps!!!
[ September 30, 2007: Message edited by: dhwani mathur ]
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think commenting the finally and changing return type to void will help the code to compile (I may be misinterpreting what you said !)


Compiler will complain something like

Unhandled exception type Exception

Why the unhandled case given by vishram is compiling and running
I think the reason behind this is the finally block returning the value to calling method no matter what happens (I could be wrong though)
[ October 01, 2007: Message edited by: prateek urmaliya ]
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is becoz the finally block would execute even in case of exception. So this makes the 'return ' reachable. Try removing the finally block and provide throws exception in method clause. Even then the compiler will complain that 'return ' is not reachable.
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its because of the return statement prevents the exception from being thrown, the compiler knows that the exception will never thrown out of the method because of the return in finally, hence doesnt require you to handle or declare the exception.
See this post for a longer discussion.
 
Vishram Khare
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dhwani,Prateek,Gurpreet and Ahmed.Thanks Ahmed for giving the link to other discussion on the same topic.Jesper Young's reply there solves all the doubt.

Thanks again.

----Vishram
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
finally block executes no matter what and also before any exceptions can be thrown... so in this case before it throws the exception, finally block executes and the return is made which completes the method...Hope this helps
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's correct. Having a 'return' statement in the finally block (which is guranteed to execute no matter what until JVM shuts down) masks the actual return statement of the method.

It is not a good practice, though.
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic