I chave create my exeption class, but when i call my exeption, show box with this exeption and program stop.
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialiazation;
namespace MyToolBar
{
public class MyXmlExeption : Exception
{
public MyXmlExeption() : base() { }
public MyXmlExeption(string message) : base(message) { }
public MyXmlExeption(string message, Exception e) : base(message, e) { }
}
}
and here i check, when i will call my exeption
foreach (XmlAttribute atr in ch.Attributes)
{
if (atr.Name == "img" && atr.Value == "")
throw new MyXmlExeption("Picture not found in xml file");
if (atr.Name == "id")
button1.Name = atr.Value;
if (atr.Name == "img")
{
button1.BackgroundImage = Image.FromFile(atr.Value);
}
if (atr.Name == "text")
button1.Text = atr.Value;
}
and here i call my exeption
thrd = new Thread(new ParameterizedThreadStart(toolBar1.XmlLoad));
try
{
//?????? ?????? ? ???????????
thrd.Start(xmlPath);
}
catch (MyXmlExeption ex) { }
Loading
Zoran HorvatPosted Jul 26, 2011, 4:57 AM
thrd.Start(xmlPath);
Instead, write a state class which contains XML path and exception, e.g. like this:
public class State
{
public State()
{
}
public State(string xmlPath)
{
_xmlPath = xmlPath;
}
public Exception Exception
{
get
{
return _exception;
}
set
{
_exception = value;
}
}
public string XmlPath
{
get
{
return _xmlPath;
}
set
{
_xmlPath = value;
}
}
private Exception _exception;
private string _xmlPath;
}
Now start the thread like this:
thrd.Start(new State(xmlPath));
Then, in the thread procedure don't miss to catch ALL exceptions:
void ThreadProc(object state)
{
State s = (State)state;
s.Exception = null; // Indicates that there was no exception
try
{
// Do work
}
catch (System.Exception ex)
{
s.Exception = ex;
}
}
Once the thread exits, you can test state's Exception property to see if there was any exception on the thread.
Zoran
Anil KumarPosted Jul 27, 2011, 2:31 AM
Mike JonsonPosted Jul 26, 2011, 3:14 PM
Zoran HorvatPosted Jul 26, 2011, 7:05 AM
Did this solve the problem?
Zoran
Zoran HorvatPosted Jul 26, 2011, 6:03 AM
foreach (XmlAttribute atr in ch.Attributes)
{
if (atr.Name == "img" && atr.Value == "")
throw new MyXmlExeption("Picture not found in xml file");
if (atr.Name == "id")
button1.Name = atr.Value;
if (atr.Name == "img")
{
button1.BackgroundImage = Image.FromFile(atr.Value);
}
if (atr.Name == "text")
button1.Text = atr.Value;
}
Actually, this foreach loop is the piece of code that I marked with // Do work.
It is only important to have throw new MyXmlException... line wrapped in try...catch (System.Exception) block.
Zoran
Mike JonsonPosted Jul 26, 2011, 5:47 AM
Zoran HorvatPosted Jul 26, 2011, 5:31 AM
thrd = new Thread(new ParameterizedThreadStart(toolBar1.XmlLoad));
try
{
//?????? ?????? ? ???????????
thrd.Start(xmlPath);
}
catch (MyXmlExeption ex) { }
Instead of thrd.Start(xmlPath) you should call thrd.Start(new State(xmlPath)).
Zoran
Mike JonsonPosted Jul 26, 2011, 5:22 AM
Mike JonsonPosted Jul 26, 2011, 4:52 AM
Zoran HorvatPosted Jul 26, 2011, 4:40 AM
You are throwing exception on one thread and catching it on another thread. That doesn't work. You have to catch the exception on the same thread on which it was thrown, and then to pass that information to the other thread yourself. Please refer to my second listing for complete solution.
Zoran
Mike JonsonPosted Jul 26, 2011, 4:38 AM
Zoran HorvatPosted Jul 26, 2011, 4:34 AM
You can help collect information about the unhandled exception on another thread using this code:
using System;
using System.Threading;
using System.Windows.Forms;
namespace ExceptionTest
{
class Program
{
static void ThreadProc()
{
throw new System.Exception();
}
static void Main(string[] args)
{
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
_thread = new Thread(new ThreadStart(ThreadProc));
_thread.Start();
_thread.Join();
Console.WriteLine("Press ENTER to continue...");
Console.ReadLine();
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Console.WriteLine(e.ExceptionObject);
}
static Thread _thread;
}
}
In this example unhandled exception will be logged, but that cannot prevent application from stopping.
The only way to keep running is to handle the exception on the thread which raises it:
try
{
foreach (XmlAttribute atr in ch.Attributes)
{
if (atr.Name == "img" && atr.Value == "")
throw new MyXmlExeption("Picture not found in xml file");
if (atr.Name == "id")
button1.Name = atr.Value;
if (atr.Name == "img")
{
button1.BackgroundImage = Image.FromFile(atr.Value);
}
if (atr.Name == "text")
button1.Text = atr.Value;
}
}
catch (MyXmlException ex)
{
// Report error
}
Reporting the error may include sending information about the exception to the caller.
Here is the modified code which handles exception on the thread and passes it nicely to the entity which has started the thread:
using System;
using System.Threading;
using System.Windows.Forms;
namespace ExceptionTest
{
class State
{
public Exception Exception
{
get
{
return _exception;
}
set
{
_exception = value;
}
}
private Exception _exception;
}
class Program
{
static void ThreadProc(object state)
{
State s = (State)state;
try
{
// Do work
throw new System.Exception();
}
catch (System.Exception ex)
{
s.Exception = ex;
}
}
static void Main(string[] args)
{
State state = new State();
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
_thread = new Thread(new ParameterizedThreadStart(ThreadProc));
_thread.Start(state);
_thread.Join();
if (state.Exception != null)
Console.WriteLine("Exception occurred on thread: {0}", state.Exception);
Console.WriteLine("Press ENTER to continue...");
Console.ReadLine();
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Console.WriteLine(e.ExceptionObject);
}
static Thread _thread;
protected Exception _threadException;
}
}
Zoran
Dorababu MekaPosted Jul 26, 2011, 4:29 AM
Mike JonsonPosted Jul 26, 2011, 4:28 AM
Dorababu MekaPosted Jul 26, 2011, 4:18 AM
catch (MyXmlExeption ex)
{
throw ex;
//You can show that using message box if you are using WINFORMS
messagebox.show(ex.tostring());
or you can write to console as follows
}