Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Try catch block.

 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does the code enter the finally block after the try block. I know that either a finally or a catch block executes after a try block. But how does the compiler know it , i mean wouldnt it be better to enter the catch block after the try block and then in the end execute the finally block ?
 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jose Please use code tags without code tags it tis really very difficult to read the code. Use it by clicking on edit button.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
remember one thing : finally block is always executable block regardless of exception.

here you Main method calls thrower method in which exceptions are thrown in all cases. but your thrower doesnt have catch block so obviuosly it travel through the finally block and the exception are caught by main method which has catch block.

to understand, introduce a catch block in your thrower method

hth
 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the this try block executed

here control goes to thrower(String s) method

But here there is no catch block therefore finally block is executed after try block. and in last this catch block is executed (i.e. after control returns from thrower(String s) method.
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first of all i agree with Pramod ...

1. you haven't written a catch sequence for the second try block

2. in the second try block ... 3rd if statement you haven't enclosed the return statement in the if block [if i suppose you wanted to]

The compiler does enters the catch block after the try block but since you haven't written it so it didn't happen.
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




I added the catch block as suggested.
Here it tells me the method must return a result of type int. The only change is that I have added a catch block after the try block.
 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to write return statement in catch block or finally block, because in method signature you given that it returns int
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are not returning anything from your method when it is defined to return an integer value ...
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here it tells me the method must return a result of type int.

You have to try to logically understand the meaning of the errors. If it tells you to return an int, why don't you ?
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jose chiramal wrote:I know that either a finally or a catch block executes after a try block.


Finally always executes, even if you have a return statement in try or catch block.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic