看来try cath 结构也不是万能的。
解决方案 »
- 请前辈指点一下C#的前景
- IIs求助。。
- 关于DatagridView和数据库的问题......
- .Net dll 如何注册(不使用regasm)
- 关于DataGridView和DataTable绑定,DataGridView点击行头排序后,DataTable行顺序未相应改变.200求解!!!
- 解释一条Sql语句:
- 在C#的Codebehind里怎么给IFRAME赋值?如何打开编辑模式?
- [求助!] 托盘图标!
- 报错!!请教
- VS.NET超级奇怪,乱报告错误?那里的问题?
- 同一个窗口类中如何在 一个按钮事件中调用另一个按钮事件?
- FileSystemWatcher监视的目录删除时 还会继续监视吗
从网上搜集的信息中有提到:
c#的try catch能捕捉的信息是在.net框架内支持的异常,而调用API等出现的异常C#根本就捕获不了,能够提示的就是哪块内存为之读不能修改之类的信息。是不是这个原因?
如果你是在其它线程出的异常,是无法捕获的
你可以通过AppDomain.CurrentDomain.UnhandledException这个方法来捕获非主线程的异常
namespace myprog
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
string MName = Process.GetCurrentProcess().MainModule.ModuleName;
string PName = Path.GetFileNameWithoutExtension(MName);
Process[] myProcess = Process.GetProcessesByName(PName);
if (myProcess.Length > 1)
{
MessageBox.Show("本程序不允许重复启动!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
try
{
//处理未捕获的异常
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
//处理UI线程异常
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
//处理非UI线程异常
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
#region 应用程序的主入口点
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmWelcome());
#endregion
}
catch (Exception ex)
{
string str = "";
string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "\r\n";
if (ex != null)
{
str = string.Format(strDateInfo + "异常类型:{0}\r\n异常消息:{1}\r\n异常信息:{2}\r\n",
ex.GetType().Name, ex.Message, ex.StackTrace);
}
else
{
str = string.Format("应用程序线程错误:{0}", ex);
}
MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
//LogManager.WriteLog(str);
}
}
} static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
string str = "";
string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "\r\n";
Exception error = e.Exception as Exception;
if (error != null)
{
str = string.Format(strDateInfo + "异常类型:{0}\r\n异常消息:{1}\r\n异常信息:{2}\r\n",
error.GetType().Name, error.Message, error.StackTrace);
}
else
{
str = string.Format("应用程序线程错误:{0}", e);
}
MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
//LogManager.WriteLog(str);
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
string str = "";
Exception error = e.ExceptionObject as Exception;
string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "\r\n";
if (error != null)
{
str = string.Format(strDateInfo + "Application UnhandledException:{0};\n\r堆栈信息:{1}", error.Message, error.StackTrace);
}
else
{
str = string.Format("Application UnhandledError:{0}", e);
}
MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
//LogManager.WriteLog(str);
}
}
}实际运行了一下,还是无法捕捉打印时出现的异常,郁闷ing ......
只能捕获 throw 的异常