|
-
Feb 28th, 2011, 03:20 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Finally block
Hi,
Scenario:
I build a UDP connection.
Within a try{} block I connect, send and receive.
Now on exceptions or invalid messages, I 'return' an error code, thus effectively exiting the method.
To clean up the connection, I want to close it.
The question here is whether I should close it before every 'return' action, or simply close it in the finally{} block.
I made the following test-case, but I'm wondering whether this is a reliable test.
Code:
static void Main(string[] args)
{
try
{
//throw new IndexOutOfRangeException();
return;
}
catch (IndexOutOfRangeException)
{
return;
}
finally
{
Console.WriteLine("Reached finally");
Console.Read();
}
}
The result of this test is that it writes 'Reached finally' whether I throw an exception or just 'return'.
I'm just a bit surprised that even if I return, it runs the finally{} block.
Is this expected behavior?
Thanks.
Delete it. They just clutter threads anyway.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|