Vijay809
Vijay809

Reputation: 9

Generating custom exception in c#

I have tried this code to raise a manual exception

 protected void test ()
    try
    {
        throw new Exception("HI");     //line22
    }
    catch (Exception ex) { lblerror.Text = ex.ToString(); }

but received exception below

System.ArgumentException: HI at Project_Test_M_Test.btnsubmit_Click(Object sender, EventArgs e) in D:\Project\Test\M_Test.aspx.cs:line 22

I want to see error message that I have send not this.

Upvotes: 1

Views: 74

Answers (2)

Don
Don

Reputation: 6882

This is what you need to do, use Message property to access the error message.

 protected void test ()
 {
     try
     {
         throw new Exception("HI");  // Exception message passed from constructor
     }
     catch (Exception ex) 
     { 
         lblerror.Text = ex.Message;
     }
}

Upvotes: 1

Kapoor
Kapoor

Reputation: 1428

Please use ex.Message instead of ex.ToString(). btw, its not a good idea to throw the base class Exception. please use a more specific one.

Upvotes: 2

Related Questions