c# - Custom message for Object reference not set to an instance of an object -
hi
have huge windows application poor exception handling. application throw object reference error lot of places , system error message showing users using message boxes.
i looking simple solution can used replace message user friendly entire application
thanks...
@anz: not use exception handling in every in code keep in mind , must know meaning of different type of exception. in scenario getting "object reference exception" , main reason of exception not checking null while accessing variable like
exa_1:-
dataset ds;
now if acces ds.table.count() give exception, here should use
dataset ds;
if(ds!=null) { int val = ds.table.count(); }
exa_2:-
string strvariable=txtinput.text; int number = convert.int32(strvariable); // here if txtinput.text empty them through exception here can use
if(!string.isnullorempty(strvariable)) int number = convert.int32(strvariable);
and if want show custom message in exception handle can create own exception class override exception class can throw , catch like:
public class myexception : exception { public string custommessage; public myexception(string sourcename) { custommessage = sourcename + " can not null"; } public myexception() { custommessage="objectreferenceexception"; } }
and in code usng try catch use
try { throw new myexception("check"); } catch (myexception ex) { messagebox.show(ex.custommessage); }
Comments
Post a Comment