手机版
你好,游客 登录 注册
背景:
阅读新闻

C#中的调试和错误处理

[日期:2012-12-05] 来源:Linux社区  作者:ghevinn [字体:大 中 小]

1、输出调试信息:

可以使用函数Debug.WriteLine();

Trace.WriteLine();

区别,在于前者只有在调试状态下才输出,后者还可以用于发布版本。

2、try...catch...finally

通过这个函数来捕获异常。

3、附加代码在vs2010中亲自测试通过

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
 
namespace ConsoleApplication9 
{ 
    class Program 
    { 
        static string[] eTypes = { "none", "simple", "index", "nested index" };  //定义异常类型数组并存入string类型的数组中 
 
 
        static void Main(string[] args) 
        { 
            foreach (string eType in eTypes)                                      //循环遍历异常类型 
            { 
                try 
                { 
                    Console.WriteLine("Main() try block reached."); 
                    Console.WriteLine("ThrowException(\"{0}\") called.", eType); 
 
                    ThrowException(eType); 
                    Console.WriteLine("Main() try block continues."); 
                } 
                catch (System.IndexOutOfRangeException e)                        //索引超出范围异常 
                { 
                    Console.WriteLine("Main() System.IndexOutOfRangeException catch" 
                        + "block reached.Message:\n\"{0}\"", e.Message); 
                } 
                catch 
                { 
                    Console.WriteLine("Main() general catch block reached.");  //普通catch捕获 
                } 
                finally                                                        //有无异常都始终会输出 
                { 
                    Console.WriteLine("Main() finally block reached."); 
                } 
 
                Console.WriteLine(); 
            } 
            Console.ReadKey(); 
        } 
 
        static void ThrowException(string exceptionType) 
        { 
            Console.WriteLine("ThrowException(\"{0}\") reached ." ,exceptionType ); 
 
            switch (exceptionType) 
            { 
                case "none":                                                    //不抛出异常 
                    Console.WriteLine("Not throwing an exception.");             
                    break; 
                case "simple":                                                  //生成一般异常 
                    Console.WriteLine("Throwing System.Exception.");             
                    throw (new System.Exception()); 
                    break; 
                case "index":                                                    //生成System.IndexOutOfRangeException.异常 
                    Console.WriteLine("Throwing System.IndexOutOfRangeException."); 
                    eTypes[4] = "error"; 
                    break; 
                case "nested index":                                            //包含自己的try块,其中调用index情况 
                    try 
                    { 
                        Console.WriteLine("ThrowException(\"nested index\")" + 
                            "try block reached."); 
                        Console.WriteLine("ThrowException (\"index\") called."); 
                        ThrowException("index"); 
                    } 
                    catch 
                    { 
                        Console.WriteLine("throwException(\"nested index\") general" 
                            + "catch block reached."); 
                    } 
                    finally 
                    { 
                        Console.WriteLine("ThrowExceptiopn(\"nested index\") finally" 
                            + " block reached."); 
                    } 
                    break; 
            } 
        } 
    } 
} 

linux
相关资讯       C# 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款