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

C#

 
QuestionSecure Web Service with C# DataSource Pin
Andre Vianna4-May-07 4:22
Andre Vianna4-May-07 4:22 
QuestionDisapear sln Pin
sujithkumarsl4-May-07 3:46
sujithkumarsl4-May-07 3:46 
AnswerRe: Disapear sln Pin
rujuc#4-May-07 4:18
rujuc#4-May-07 4:18 
AnswerRe: Disapear sln Pin
marky7774-May-07 5:03
marky7774-May-07 5:03 
QuestionReading HashTable ? Pin
Software_Specialist4-May-07 2:09
Software_Specialist4-May-07 2:09 
AnswerRe: Reading HashTable ? Pin
rah_sin4-May-07 2:35
professionalrah_sin4-May-07 2:35 
GeneralRe: Reading HashTable ? Pin
Software_Specialist4-May-07 4:15
Software_Specialist4-May-07 4:15 
QuestionException Handling: Object creation within try block Pin
Goebel4-May-07 2:03
Goebel4-May-07 2:03 
I'm currently getting into exception handling and have encountered the following problem. Let's say we have a class SampleClass. The SampleClass constructor takes various input parameters and is to check whether these are valid. If not exceptions might be thrown. Ideally I would like to have the code creating an object of type SampleClass to handle the potential exceptions. This does not quite work, as the compiler reports "Use of unassigned local variable..." outside the try block.

Here's the code:

<code>
public class SampleClass
{
public SampleClass(int param1, int param2)
{
int param1;
int param2;

if (param1 % param2 != 0)
{
throw new Exception();
}

this.param1 = param1;
this.param2 = param2;
}

public void Action()
{
// Some weird stuff in here.
}
}

public class Tester
{
class Program
{
static void Main(string[] args)
{
SampleClass s;

try
{
s = new SampleClass(20, 20);
}
catch (Exception e)
{
// Do processing here.
}

s.Action(); // Compiler says: Use of unassigned local variable 's'
}
}
}
</code>

I'd be quite pleased if someone could direct me into a promising direction. How to create object instances within try? You might say, just incorporate s.Action() within the try block. The problem is that in the real world example there is lots of code referencing 's'. So I'd end up with a massive try block and dozens of catch blocks afterwards. I reckon that's not what you would call best practices.

Thanks in advance.
All the best.
Goebel
AnswerRe: Exception Handling: Object creation within try block Pin
Pete O'Hanlon4-May-07 2:09
mvePete O'Hanlon4-May-07 2:09 
GeneralRe: Exception Handling: Object creation within try block Pin
Goebel4-May-07 2:23
Goebel4-May-07 2:23 
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 
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 

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.