Link to home
Start Free TrialLog in
Avatar of lankapala
lankapala

asked on

C# Error handling issue

Hi,

I have write Assembly using C# stored procedure (web services), it's working fine.
Only issue with Error Handling.
some errors i can catch (if Order Exisits) but most of Errors i can't catch. but when i was testing in the
unit test i can see it's working preoprly.But when after comipled and create assembly
most of Errors Can't track.
Any idea appriciated.
//property
        public static string ReturnMsg { get; set; }

//Error Catching part
catch (Exception ex)
            {
                //pipe.Send(ex.ToString());
                //pipe.Send(strValue + "\n" + strcount);
                //Get stack trace for the exception with source file information
                var st = new StackTrace(ex, true);
                // Get the top stack frame
                var frame = st.GetFrame(0);
                // Get the line number from the stack frame
                var line = frame.GetFileLineNumber();

                strcount = "Line Number " + line.ToString() + "-->" + ex.ToString();//ex.Message.ToString() +
                //pipe.Send(strValue + "\n" + strcount);
                ReturnMsg = ex.Message.ToString();
               //throw new Exception("Error: " + ex.Message);
            }

Open in new window


Note: i have check many examples. stackflows.But all are not working. I knew how to search and google find answer.
i would appriciate if any one show me or direct me any idea what's im doing wrong
Avatar of Nakul Vachhrajani
Nakul Vachhrajani
Flag of India image

catch{} works when an exception is generated within a try{} block. Hence, Where is the corresponding try{} block in the sample code you shared?
Avatar of lankapala
lankapala

ASKER

yes,try code already available(without try code you can't run the catch).
try
{
TestOMs t1=new TestMOS1();
Orders newOrders=new newOrders(); //class
newOrders.Firstname="Test1";
newOrders.Lastname="Test2";
 t1.AddOrder(newOrders);
  ReturnMsg = "Successfully Added";
}
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.