Some question on c# Exception

Status
Not open for further replies.

maniac84

Full Member level 6
Joined
Mar 4, 2012
Messages
337
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Activity points
3,661
Hi guys,
Recently been studying c# coding. Meet up with the code below:
Code:
           catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");      
                
            }

This code will show the error message : Object reference not set to an instance of an object.

May I know what is the significance of this error message?
How do I replace it with my own error message?
 

check the point where this error is occurred by debugging the code (press F10 key)

I guess that this should be not initialize the object, something like
Code:
x = new x();
 

hey maniac84,

it seemed your code is working fine; the error message "Object reference not set to an instance of an object" is presumably the Exception (error) caught by your catch statement.

catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");

}

to show your user defined error message instead, simple replace the "ex.Message" parameter of the MessageBox.Show() method with a string of your defined error message.

that is,

catch (Exception ex)
{
MessageBox.Show("you defined error message", "Error");

}

Hope this is helpful? Don't hesitate to ask for clarifications if you are still confused!
 
Last edited:

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…