zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C#winform各种异常处理方式

c#Winform异常 处理 方式 各种
2023-09-14 09:10:03 时间

C# winform 窗体程序异常案例【详细】

using System;
using System.Windows.Forms;

namespace test_1_1118
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //使用.NET默认异常处理机制捕获异常
        private void button1_Click(object sender, EventArgs e)
        {
            int a = 0;
            int result = 100 / a;
            MessageBox.Show(result.ToString());
        }

        //使用try...catch手动捕获异常
        private void button2_Click(object sender, EventArgs e)
        {
            int a = 0;
            int result = 0;
            try
            {
                result = 100 / a;
                MessageBox.Show("这里不会执行");
            }
            catch (DivideByZeroException)
            {
                MessageBox.Show("出现异常,除数不能为0");
            }
           

        }

        //使用try...catch...finally手动捕获异常
        private void button3_Click(object sender, EventArgs e)
        {
            int a = 0;
            int result = 0;
            try
            {
                result = 100 / a;
                MessageBox.Show("这里不会执行");
            }
            catch (DivideByZeroException)
            {
                MessageBox.Show("出现异常,除数不能为0");
            }
            finally
            {
                //finally语句块中的内容一定会被执行
                MessageBox.Show("finally语句块已执行");
            }

        }

        // 使用try...多个catch...finally手动捕获异常
        //只要有一个catch语句块捕获到异常,其它catch语句块不执行。
        private void button4_Click(object sender, EventArgs e)
        {
            int a = 0;
            int result = 0;
            try
            {
                result = 100 / a;
                MessageBox.Show("这里不会执行");
            }
            catch (DivideByZeroException)
            {
                MessageBox.Show("出现异常,除数不能为0");
            }
            catch(Exception ex)
            {
                //只要有一个catch语句块捕获到异常,其它catch语句块不执行
                MessageBox.Show("异常");
            }
            finally
            {
                MessageBox.Show("finally语句块已执行");
            }
        }

        //使用try...catch(不带括号,不带参数)手动捕获异常
        //可以捕获任何异常
        private void button5_Click(object sender, EventArgs e)
        {
            int a = 0;
            int result = 0;
            try
            {
                result = 100 / a;
                MessageBox.Show("这里不会执行");
            }
            catch
            {
                MessageBox.Show("异常");
            }
        }

        //抛出异常本身并没有显示
        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                throw new DivideByZeroException("除数不能为零!");//不输出
            }
            catch(DivideByZeroException exception)
            {
                MessageBox.Show("异常");
            }
            MessageBox.Show("啦啦啦啦啦");
        }



        public class Caculate
        {
            public void Divide()
            {
                try
                {
                    int a = 0;
                    int result = 100 / a;
                }
                catch(DivideByZeroException exception)
                {
                    throw;
                }
            }
        }
        //在Calculate内部抛出的异常,被更高层次的客户端捕获
        private void button7_Click(object sender, EventArgs e)
        {
            Caculate c = new Caculate();
            try
            {
                c.Divide();
            }
            catch(Exception exception)
            {
                MessageBox.Show("捕获异常");
            }
            MessageBox.Show("啦啦啦啦");
        }

        //自定义异常
        private void button8_Click(object sender, EventArgs e)
        {
            try
            {
                throw new MyException("i am exception");
            }
            catch(Exception exception)
            {
                MessageBox.Show("捕获到自定义异常了!");
            }
            MessageBox.Show("啦啦啦啦");
        }
        public class MyException : Exception
        {
            public MyException(string str)
            {
                MessageBox.Show("这是我的自定义异常" + str);
            }
        }
    }
}

总结:

.NET异常处理并不是标准的try…catch…finally,可以是很灵活的。
尽量在较低层次抛异常,在较高层次捕获异常。