This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Try catch block. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Try catch block." Watch "Try catch block." New topic
Author

Try catch block.

jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
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

Joined: Jul 15, 2008
Posts: 629

jose Please use code tags without code tags it tis really very difficult to read the code. Use it by clicking on edit button.


Life is easy because we write the source code.....
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jul 15, 2008
Posts: 629

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

Joined: Jun 08, 2010
Posts: 383

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.


http://plainoldjavaobject.blogspot.in
jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266




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

Joined: Jul 15, 2008
Posts: 629

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

Joined: Jun 08, 2010
Posts: 383

you are not returning anything from your method when it is defined to return an integer value ...
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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 ?


[My Blog]
All roads lead to JavaRanch
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
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.


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Try catch block.
 
Similar Threads
out of scope expection
out of scope expection
finally clause...
Concept on coding for Exception Handling