异常处理

不要进行无意义的try....catch,只在真的需要catch的地方再处理。应彻底在测试阶段就消灭异常。

代码中没有必要每个地方都try...cvatch 

程序中出现未处理的异常会直接退出,每个地方都try ,,..catch....太麻烦,可以在App中处理      DispatcherUnhandleException

新建一个窗体App .xaml.代码如下:

<Application x:Class="HRMSys.UI.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml" DispatcherUnhandledException="Application_DispatcherUnhandledException">
    <Application.Resources>
         
    </Application.Resources>
</Application>

 

 在App .xaml.cs中添加代码如下:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;

namespace HRMSys.UI
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            //在Application_DispatcherUnhandledException中集中处理异常
            MessageBox.Show("程序中出现了严重错误,请联系系统开发商!"+e.Exception.Message);
            e.Handled = true;
        }
    }
}

完毕!

posted @ 2013-08-07 23:17  秋水惜朝  阅读(209)  评论(0编辑  收藏  举报