Click here to Skip to main content
15,919,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code has a thread inside thread. I used try catch block, so when an exception occurs, it will be shown in label like...
C#
ThreadStart threadstartprint = () => CreatePDF();
Thread threadprint = new Thread(threadstartprint);

CreatePDF()
{
     try
     {
     }
     catch(Exception ex)
     {
          lbl.text=ex.message;
     }
}

My problem is that even if an exception is occurred, but it is not showed.
Is there any way to catch exception in thread and show that exception ?
Posted
v3
Comments
Tejas Vaishnav 28-Aug-12 6:46am    
try to put break point to your thread catch part and find out it will stops there or not. if it will there then try to debug that and check what was the lbl.text after it will executed.

1 solution

You have not started your thread so control is not invoking CreatePDF() method Which i am getting by your given code.

Your updated code should be like below :- Try this one may help...
C#
ThreadStart threadstartprint = () => CreatePDF();
Thread threadprint = new Thread(threadstartprint);
threadprint.Start();

CreatePDF()
{
     try
     {
     }
     catch(Exception ex)
     {
          lbl.text=ex.message;
     }
}
 
Share this answer
 
v2
Comments
Pratika05 28-Aug-12 7:19am    
i have started it but not included that line here but again exception is not printed in label
Sunil Kumar Pandab 29-Aug-12 3:40am    
Whatever data you have given, I have tested in my system it works. Check this below:-
ThreadStart threadstartprint = () => CreatePDF();
Thread threadprint = new Thread(threadstartprint);
threadprint.Start();

public void CreatePDF()
{
try
{
throw new IndexOutOfRangeException("Exception Occurs");
}
catch(Exception ex)
{
lbl.Text=ex.Message;
}
}

Your code is working fine. If you are not sucseeding then let us know your actually code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900