zoukankan      html  css  js  c++  java
  • 【.net 深呼吸】细说CodeDom(10):生成异常处理语句

    写完这一篇,大概可以准备过年了,就算是这系列文章的收尾吧。

    异常处理语句,就是常说的try...catch语句,有时候,也会带有finally子句。要生成异常处理语句,得用到CodeTryCatchFinallyStatement类,它包含三个部分。

    1、TryStatements:尝试执行的代码块。

    2、CatchClauses:捕捉异常的代码块。CatchClauses是一个子句集合,因为一个try语句可以包含N个catch子句,而每个catch块都由CodeCatchClause类来表示,使用时应提供要捕捉的异常的类型,异常对象的临时变量名,以及catch块的语句集合。

    3、FinallyStatements:finally语句块,不管会不会发生异常,finally中的语句会执行。

    下面看一个最常见的try语句的生成。

                CodeTryCatchFinallyStatement trycatfanStatement = new CodeTryCatchFinallyStatement();
                trycatfanStatement.TryStatements.Add(new CodeCommentStatement("试着执行"));
                CodeCatchClause catClause = new CodeCatchClause();
                // 异常类型
                catClause.CatchExceptionType = new CodeTypeReference(typeof(Exception));
                // 临时变量名
                catClause.LocalName = "ex";
                // catch块中的语句
                catClause.Statements.Add(new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(typeof(Console)), nameof(Console.WriteLine)), new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("ex"), nameof(Exception.Message))));
                // 记得将 CodeCatchClause 对象加入到 CatchClauses 集合中
                trycatfanStatement.CatchClauses.Add(catClause);
    
                CodeDomProvider provider = CodeDomProvider.CreateProvider("cs");
                provider.GenerateCodeFromStatement(trycatfanStatement, Console.Out, null);

    以上代码只生成了try和catch两个子块,这种形式是最为常见的。其生成的代码如下。

    当然,如果需要,还可以添加上 finally 块,在上面的示例代码中加入以下代码:

                trycatfanStatement.FinallyStatements.Add(new CodeCommentStatement("清理操作"));

    然后生成的代码中就会包含 finally 块了。

    try 语句可以包括多个 catch 子句,比如这样。

                CodeTryCatchFinallyStatement trystatement = new CodeTryCatchFinallyStatement();
                trystatement.TryStatements.Add(new CodeCommentStatement("待执行代码"));
                // 第一个 catch 子句
                CodeCatchClause catch1 = new CodeCatchClause();
                catch1.CatchExceptionType = new CodeTypeReference(typeof(FormatException));
                catch1.LocalName = "fex";
                catch1.Statements.Add(new CodeCommentStatement("捕捉异常"));
                trystatement.CatchClauses.Add(catch1);
                // 第二个 catch 子句
                CodeCatchClause catch2 = new CodeCatchClause();
                catch2.CatchExceptionType = new CodeTypeReference(typeof(ArgumentException));
                catch2.LocalName = "gex";
                catch2.Statements.Add(new CodeCommentStatement("捕捉异常"));
                trystatement.CatchClauses.Add(catch2);
    
                CodeDomProvider p = CodeDomProvider.CreateProvider("C#");
                p.GenerateCodeFromStatement(trystatement, Console.Out, null);

    以上代码生成的try语句包含两个catch子句,分别捕捉FormatException和ArgumentException两种类型的异常。生成的代码如下。

    顺便也说说抛出异常的语句,使用的是 CodeThrowExceptionStatement 类,例如

                CodeThrowExceptionStatement ts = new CodeThrowExceptionStatement(new CodeObjectCreateExpression(typeof(FieldAccessException)));

    生成的throw语句如下图所示。

    传递给 CodeThrowExceptionStatement 构造函数的参数为要抛出的异常对象,本例直接用new关键字来创建异常实例。如果明确定义了异常变量,可以引用变量。就像这样。

                CodeVariableDeclarationStatement vd = new CodeVariableDeclarationStatement(typeof(EncoderFallbackException), "ex", new CodeObjectCreateExpression(typeof(EncoderFallbackException)));
                CodeThrowExceptionStatement ts = new CodeThrowExceptionStatement(new CodeVariableReferenceExpression("ex"));
    
                CodeDomProvider p = CodeDomProvider.CreateProvider("cs");
                p.GenerateCodeFromStatement(vd, Console.Out, null);
                p.GenerateCodeFromStatement(ts, Console.Out, null);

    生成的代码如下。

     ===========================================

    好了,Code DOM 系列文章就写完了。

    过年后,如果能赶上 Windows 10 “红石2”的更新,那咱们就继续聊 UWP 相关的内容。

    提前祝大伙新春快乐。

  • 相关阅读:
    Python爬虫入门一之综述
    关于 PHP 7 你必须知道的五件事
    10个用于Web开发的最好 Python 框架
    如何用Python编写一个聊天室
    无需操作系统直接运行 Python 代码
    使用Python开发chrome插件
    Python下用Scrapy和MongoDB构建爬虫系统(1)
    python 目录操作
    用主题模型可视化分析911新闻(Python版)
    React Native通信机制详解
  • 原文地址:https://www.cnblogs.com/tcjiaan/p/6294084.html
Copyright © 2011-2022 走看看