06面向对象高级特性xm.ppt
文本预览下载声明
C#高级编程 调试 调试的必要性 错误类型 调试过程 调试过程 调试过程 调试过程 调试过程 VS.NET 中的调试工具 VS.NET 中的调试工具 VS.NET 的调试工具 VS.NET 中的调试工具 VS.NET 中的调试工具 异常 C# 中的异常处理 C# 中的异常处理 错误与异常 错误:可预见,如信用卡号格式不对或口令不对。可由程序代码进行排除。 异常:与程序无关的外部原因造成。如数据表不可用或硬件故障等。 System.Exception System.Exception System.Exception try 和 catch 块 try 和 catch 块 try 和 catch 块 try 和 catch 块 使用 finally 多重 catch 块 using System; public class TestExcep { public static int Calc(int j) { return 100 / j; } } class MyApp { public static void Main() { TestExcep exTest = new TestExcep(); try { int dZero = TestExcep.Calc(0); Console.WriteLine(Result: {0}, dZero); } catch (DivideByZeroException ex) { Console.WriteLine(ex.Message: {0}, ex.Message); Console.WriteLine(ex.Source: {0}, ex.Source); Console.WriteLine(ex.TargetSite: {0}, ex.TargetSite.ToString()); Console.WriteLine(ex.StackTrace: {0}, ex.StackTrace); } catch (Exception ex) { Console.WriteLine(General + ex.Message); } finally { Console.WriteLine(Cleanup occurs here); } } } 如何创建定制异常类 using System; public class NoDescException : ApplicationException { public NoDescException() { } public NoDescException(string message) : base(message) { } public NoDescException(string message, Exception innerEx) : base(message, innerEx) { } } public interface IFun1 { string ShowMe();} public interface IFun2 { string ShowMe(); } class Circle : IFun1 { public string ShowMe() { return Circle-IFun1; } } public class ObjShowMe { public static void ShowMe(object obj) { if (!(obj is IFun1 obj is IFun2)) { throw new NoDescException(Interface not implemented for + obj.ToString()); } } } public class MyApp { static void Main() { Circle myCir = new Circle(); try {
显示全部