473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

return statement in finally block

public int divme(int x, int y)
{
int z;
try
{
z = x / y;
}
catch (Exception ex)
{
z = 0;
}
finally
{
return z;
}
}
this doesnot compile, it says "Control cannot leave the body of a finally clause"

can some one please explain me.

Thanks in advance for your soution and time.
Oct 30 '07 #1
6 4805
Plater
7,872 Expert 4TB
Why do you even need a finally block?

Finally{} is executed if there's an error or not.

So this:
Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.   //some try stuff
  4. }
  5. catch(Exception ee)
  6. {
  7.   //some catch stuff
  8. }
  9. finally
  10. {
  11.    //this will happen regardless
  12. }
  13.  
is the same as this:
Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.   //some try stuff
  4. }
  5. catch(Exception ee)
  6. {
  7.   //some catch stuff
  8. }
  9. //this will happen regardless
  10.  
Just move your return statement below it.
Oct 30 '07 #2
r035198x
13,262 8TB
Why do you even need a finally block?

Finally{} is executed if there's an error or not.

So this:
Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.   //some try stuff
  4. }
  5. catch(Exception ee)
  6. {
  7.   //some catch stuff
  8. }
  9. finally
  10. {
  11.    //this will happen regardless
  12. }
  13.  
is the same as this:
Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.   //some try stuff
  4. }
  5. catch(Exception ee)
  6. {
  7.   //some catch stuff
  8. }
  9. //this will happen regardless
  10.  
Just move your return statement below it.
In the second case the code after the catch may not be executed depending on what is done in the catch (If e.g. a return is encountered in the catch).

@OP You cannot return/break/continue or put a goto inside the finally block by definition.
Oct 30 '07 #3
Plater
7,872 Expert 4TB
In the second case the code after the catch may not be executed depending on what is done in the catch (If e.g. a return is encountered in the catch).

@OP You cannot return/break/continue or put a goto inside the finally block by definition.
If there was a Return statement in the catch{} block, the finaly{} wouldn't be executed anyway would it?
I've never used finaly{} because I never found a use for it, so I'm only just guessing really.
Oct 30 '07 #4
balabaster
797 Expert 512MB
You should really only be using the finally block to dispose of objects that are instantiated in your Try clause...so for instance killing your references to class objects that would otherwise leak memory etc. There is really no need for a return in your finally clause though as previously mentioned. Just thought I would chip in my 2 cents as to what the finally block should be used for. It just keeps blocks of code logically together for organizational purposes which makes for tidier code.
Oct 30 '07 #5
Plater
7,872 Expert 4TB
Aye.
For a given function I try to follow this pattern:

Expand|Select|Wrap|Line Numbers
  1. private <returntype> SomeFunction()
  2. {
  3.    <returntype> retval = <null or someother "null-like" value>;
  4.    try
  5.    {
  6.       //stuff
  7.       //last thing done is set retval to a valid value
  8.    }
  9.    catch(Exception ee)
  10.    {//trap all Exception types
  11.       //exception handling
  12.    }
  13.    return retval;
  14. }
  15.  
Oct 30 '07 #6
r035198x
13,262 8TB
If there was a Return statement in the catch{} block, the finaly{} wouldn't be executed anyway would it?
I've never used finaly{} because I never found a use for it, so I'm only just guessing really.
Nope. It would be executed

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. class FinallyTest {
  3.     public static void Main() {
  4.         try {
  5.             Object stupidNull = null;
  6.             Console.WriteLine(stupidNull.ToString());
  7.         }
  8.         catch(Exception e) {
  9.              Console.WriteLine("caught");
  10.              return;
  11.         }
  12.         finally {
  13.             Console.WriteLine("finally");
  14.         }
  15.     }
  16. }
Prints
Expand|Select|Wrap|Line Numbers
  1. caught
  2. finally
finally is always executed.
Oct 30 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: wk6pack | last post by:
Hi, I was wondering why when I declare the dim variable outside the try statement, I could use the .dispose() function but when I declare it inside the try statement, I get Name 'varname' is not...
8
by: Daniel Billingsley | last post by:
Suppose I have a method that returns some type of object, and in that method I have a try...catch block and just throw my own exception when I catch one. The compiler insists that all code paths...
3
by: Robert May | last post by:
Question, Say I have the following: Using(DataSet ds=new DataSet) { ... da.fill(ds) ... return ds;
16
by: Esteban404 | last post by:
I have a Winform news kiosk app which connects to a SQL database. When the main form is running, it reaches a threshold time or number of sequential operations and then loads another instance with...
3
by: LP | last post by:
Hi, Considering the following code: using(mySQLconn) { if (someCondition==true) return 0; //more code after return //do some db calls
16
by: Chris | last post by:
Hi, I am using the try catch finally block in many places in my code. When a create an sqldatareader dr and try to close it in my finally block I get the error: use of unsigned local variable....
2
by: Robert Bravery | last post by:
Hi all, Being new to C# and .net I often don't know how to use things. I have created an app that imports excel data, it works well, with methods to open excel, extract the data and close excel....
11
by: =?Utf-8?B?Um9nZXIgVHJhbmNoZXo=?= | last post by:
Hello, I have a question about the infamous GOTO statement and the way to return a result from a sub: I have a sub that has to make some calls to external COM methods, and because these...
12
by: Matt B | last post by:
I was just wondering if there is a "best" choice from the following couple of ways of returning a value from a method: 1) private HashAlgorithm GetSpecificHashAlgorithm(string hashString){ if...
5
by: Hillbilly | last post by:
MSDN Remarks "as a rule" the using statement should be used when instantiating objects which inherit IDisposable. Other than the obvious unmanaged objects like the file system example, fonts and...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.