c#异常处理的形式和原则

一、异常处理形式

1.及时处理:发现异常随即处理并恢复程序。
2.抛出异常:捕获异常后,重新封装异常信息或直接抛给调用者

二、编写异常处理的原则

在可能发生异常的地方添加异常处理方法

三、异常对象提供的重要信息

属性名称 说明
Source 此属性表示导致异常发生的应用程序或对象的名称
Message 提供引起异常的详细信息
StackTrace 此属性提供在堆栈上所调用方法的详细信息,并首先显示最近调用的方法

四、异常处理

下面以一次数据库的删除为例子。
前提条件:该条删除语句删除的记录存在外键约束
这里我是三层结构先贴出来,未做处理异常的代码
底层

  class SQLHelper{
        public static int Update(string sql)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            int result = cmd.ExecuteNonQuery();
            conn.Close();
            return  result;
        }
   }

Service层

class StudentService
    {
        public int DeleteStudentById(string studentId)
        {
            string sql = "delete from Students where StudentId=" + studentId;
            return SQLHelper.Update(sql);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            StudentService studentService = new StudentService();
            studentService.DeleteStudentById("100001");
            Console.ReadKey();

        }
    }

上面的代码运行,会报以下错误
这里写图片描述

下面我们进行异常的处理
从底层开始。底层将第一次捕获的异常传递给上级调用者进一步详细处理,一般在底层的抛出异常处会加日志的存储

 public static int Update(string sql)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql, conn);

            try
            {
                conn.Open();
                int result = cmd.ExecuteNonQuery();
                return result;
            }
            catch(Exception e)
            {
                //将异常信息保存在日志中。。。
                throw e;
            }
            finally
            {
                conn.Close();
            }


        }

调用者怎么进一步处理异常呢?我们来看下面的处理,e.Number==547 ,547表示外键约束异常

        public int DeleteStudentById(string studentId)
        {
            string sql = "delete from Students where StudentId=" + studentId;
            try
            {
                return SQLHelper.Update(sql);
            }
            catch(SqlException e)
            {
                if (e.Number == 547)
                    throw new Exception("该学员在其他数据表使用,不能被删除");
                else
                    throw new Exception("数据库操作出现问题,不能执行删除!具体信息:" + e.Message);

            }
            catch (Exception e)
            {
                throw new Exception("数据库操作出现问题,不能执行删除!具体信息:" + e.Message);
            }

        }

1.多路捕获异常不是必须的,只有需要的时候才使用
2.可以添加多个catch块
3. 一定要把Exception类放在最后

下来看前端的处理

class Program
    {
        static void Main(string[] args)
        {
            StudentService studentService = new StudentService();
            try
            {
                int result = studentService.DeleteStudentById("100001");
                if (result == 1)
                {
                    Console.WriteLine("删除成功");
                }
                else
                {
                    Console.WriteLine("删除失败");
                }
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }
            Console.ReadKey();

        }
    }

猜你喜欢

转载自blog.csdn.net/Maybe_ch/article/details/82216931