Click here to Skip to main content
15,891,316 members
Home / Discussions / C#
   

C#

 
QuestionException Handling: Object creation within try block Pin
Goebel4-May-07 2:03
Goebel4-May-07 2:03 
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 
If you provide a default constructor, I would recommend not to use a second constructor for your purpose. Instead provide a method.
Currently you assign a new object within the try/catch-block and I'm not sure what happens to s, if an exception is thrown within the second constructor.

Goebel wrote:

public class SampleClass
{
    public SampleClass()
    {
        this.param1 = 1;
        this.param2 = 1; 
    }

    public Assign(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 = new SampleClass(); 

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

            s.Action();
        }
    }
}





Regards,
Tim

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 
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 

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.