1

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.

4
  • Why are you using exceptions in this way, Exceptions are for unexpected, unrecoverable errors. They should not be used to control program flow in a way that showing ex.Message to the user would be useful. Please read the MSDN guidelines on throwing exceptions Commented Oct 17, 2015 at 7:49
  • Also, that is not a custom exception, here is a link to the MSDN on how to create a actual custom exception. Commented Oct 17, 2015 at 7:52
  • Thanks for suggestion but I am a student and personally think It's a better way to learn generating exception and their handling. But I will remember your suggestions and will be back if stuck anywhere else. Thanks once again. Commented Oct 19, 2015 at 5:20
  • and this was just an idea whether I can use it or not. Commented Oct 19, 2015 at 5:23

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

And possibly even the InnerException if you want more information for debugging
@DieterB ex.ToString() contains the details of InnerException. @Kapor Teaching people to use ex.Message is a bad habit to get people to start doing, you loose a lot of debugging info
@ScottChamberlain, I suspect there is some misunderstanding, my point is that in the string passed in the constructor ("HI" in sample code) is available in readonly property ex.Message. And that is what was asked in this question - "I want to see error message that I have send not this.".
@ScottChamberlain, As far as usage of Message is concerened, As per my understanding - It should contain helpful text indicating why the exception was thrown. Ideally end users should not see this but it should be logged. Therefor it can be technical so that the developers viewing the logs can use the information in the message to fix the code when producing a new version. I'm sorry, but I did not get your point. Do you mean that I should have also told the usage of message property ? (Which I have written in this comment)?
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.