Click here to Skip to main content
16,006,531 members
Home / Discussions / C#
   

C#

 
GeneralRe: Exception Handling: Object creation within try block Pin
Pete O'Hanlon4-May-07 2:27
mvePete O'Hanlon4-May-07 2:27 
GeneralRe: Exception Handling: Object creation within try block Pin
Goebel4-May-07 2:47
Goebel4-May-07 2:47 
AnswerRe: Exception Handling: Object creation within try block Pin
Tim Paaschen4-May-07 2:13
Tim Paaschen4-May-07 2:13 
GeneralRe: Exception Handling: Object creation within try block Pin
Goebel4-May-07 2:33
Goebel4-May-07 2:33 
GeneralRe: Exception Handling: Object creation within try block Pin
Tim Paaschen4-May-07 2:48
Tim Paaschen4-May-07 2:48 
GeneralRe: Exception Handling: Object creation within try block Pin
Goebel4-May-07 2:57
Goebel4-May-07 2:57 
AnswerRe: Exception Handling: Object creation within try block Pin
AFSEKI7-May-07 3:22
AFSEKI7-May-07 3:22 
AnswerRe: Exception Handling: Object creation within try block [modified] Pin
gumi_r@msn.com7-May-07 6:31
gumi_r@msn.com7-May-07 6:31 
The error you are getting is simply the compiler warning you that you are using a variable that may not be explicitly initialized during runtime depending which path the execution flow takes.

In your case, you are using an uninitialised variable named s. The compiler is simply detecting that the execution might reach the catch and finally blocks with s never being explicitly initialized in your code and will therefore warn you because it's not sure if you know what you're doing (in your code this would happen if your class constructor throws an exception).

On the other hand, if you explicitly initialize the variable s to null, which makes no difference at runtime to what you originally coded, the compiler understands that you know what you are doing and that you have factored in the possibility of s being null if the catch and finally blocks are ever executed.

Please note that this is not something specific to try catch-blocks and constructors. You would get the same error in the following code (the code does not make much sense at all but its a valid example):

public void example(bool dummy)
           {
                myClass s;

                if (dummy)
                {
                    s = new myClass();
                }
                else
                {
                    if (s==null)
                        //do something
                    else
                        //do something else
                }
                    
           }


Hope this clears it up a little for you.

P.D. About the default constructor, C# does not work the same as C++. Having a default constructor will not make your variable s use it when its defined:

myClass s; //this will not call the default constructor as in C++. C# initializes objects to null.

Please note that the compiler error is not really necessary as the s variable is truely null even if not explicitly initialized and will therefore not cause unexpected behaviour reading uninitialised memory, etc. Its more an aid and a reminder to the coder that might be overlooking a possible execution path where the variable may not have the expected value. This becomes evident if instead of using your class you try your same code with int s; You will still get exactly the same error even though s is by default initialized to 0 and has a perfectly valid value.





-- modified at 13:11 Monday 7th May, 2007
QuestionNeed solution Pin
DON3454-May-07 1:58
DON3454-May-07 1:58 
AnswerRe: Need solution Pin
Pete O'Hanlon4-May-07 2:13
mvePete O'Hanlon4-May-07 2:13 
GeneralRe: Need solution Pin
Paul Conrad4-May-07 13:55
professionalPaul Conrad4-May-07 13:55 
AnswerRe: Need solution Pin
Sandeep Akhare4-May-07 3:06
Sandeep Akhare4-May-07 3:06 
GeneralRe: Need solution Pin
DON3454-May-07 3:52
DON3454-May-07 3:52 
GeneralRe: Need solution Pin
Sandeep Akhare4-May-07 3:55
Sandeep Akhare4-May-07 3:55 
AnswerRe: Need solution Pin
Sandeep Akhare4-May-07 4:47
Sandeep Akhare4-May-07 4:47 
GeneralRe: Need solution Pin
jayart4-May-07 6:55
jayart4-May-07 6:55 
GeneralRe: Need solution Pin
Sandeep Akhare6-May-07 19:28
Sandeep Akhare6-May-07 19:28 
GeneralRe: Need solution Pin
DON3454-May-07 18:54
DON3454-May-07 18:54 
GeneralRe: Need solution Pin
Sandeep Akhare6-May-07 19:35
Sandeep Akhare6-May-07 19:35 
QuestionHow to catch "global KeyDown" event ? Pin
stancrm4-May-07 1:37
stancrm4-May-07 1:37 
AnswerRe: How to catch "global KeyDown" event ? Pin
Giorgi Dalakishvili4-May-07 1:43
mentorGiorgi Dalakishvili4-May-07 1:43 
GeneralRe: How to catch "global KeyDown" event ? Pin
stancrm4-May-07 1:59
stancrm4-May-07 1:59 
QuestionDynamic Report Column headings problem Pin
Dawie J4-May-07 1:23
Dawie J4-May-07 1:23 
Questionajax implementation for dynamically created dropdown lists Pin
skop84-May-07 0:29
skop84-May-07 0:29 
Questionhow to explorer Folder Option ,...(Urgent) Pin
Sanjib Raj3-May-07 23:52
Sanjib Raj3-May-07 23:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.