重学c#系列

举个例子:

static void Main(string[] args)

{

FileStream sw = null;

try

{

  sw = new FileStream(@"C:\test\test.txt", FileMode.Open, FileAccess.ReadWrite)

  sw.Write(new byte[1]);

}

catch (DirectoryNotFoundException ex)

{

//文件目录没有找到异常

Console.WriteLine(ex);

}

catch (FileNotFoundException ex)

{

//文件没有找到异常

Console.WriteLine(ex);

}

catch (IOException ex)

{

//io操作异常

Console.WriteLine(ex);

}

finally

{

sw?.Flush();

sw?.Close();

}

}

没有找到找到相应的catch,那么会一步一步往下找。

首先是查看DirectoryNotFoundException ,然后是FileNotFoundException,只要是io操作出现问题都会是IOException。

那么我该如何知道DirectoryNotFoundException、FileNotFoundException、IOException他们的关系呢?

这时候就需要看图了,图不大,可以记下来。

如图:

上面这些是常用的,可以说是必会的吧,下面介绍一下他们的作用。

异常 详细

ArgumentException 方法参数异常

ArgumentNullException 参数为空异常

ArgumentOutOfRangeException 索引小于零或超出数组边界时,尝试对数组编制索引时引发

ArithmeticException 算术运算期间出现的异常的基类,例如 DivideByZeroException 和 OverflowException。

OverflowException 当在检查的上下文中执行的算术、强制转换或转换运算导致溢出时引发的异常

StackOverflowException 执行堆栈由于有过多挂起的方法调用而用尽时引发;通常表示非常深的递归或无限递归。

IOException 发生I/O错误时引发的异常。

FileLoadException 找到托管程序集但不能加载时引发的异常

FileNotFoundException 文件没有找到

EndOfSteamExcetion 读操作试图超出流的末尾时引发的异常。

DriveNotFoundException 当尝试访问的驱动器或共享不可用时引发的异常。

ApplicationException 用作应用程序定义的异常的基类

TargetInvocationException 由通过反射调用的方法引发的异常

CompositionException 表示在 CompositionContainer 对象中进行组合期间发生一个或多个错误时引发的异常。

ChangeRejectedException 一个指示部件在组合期间是否已遭拒绝的异常。

有些异常是我们写代码不应该去产生异常的,比如说ApplicationException作为基类的异常,如果出现这些异常一般是我们代码写的有问题,

而不是我们的主观因素。应从 Exception 类(而不是 ApplicationException 类)派生自定义异常。 不应在代码中引发 ApplicationException 异常,除非你

打算重新引发原始异常,否则不应捕获 ApplicationException 异常。 这里举个例子:TargetInvocationException的基类是ApplicationException,这个是

由通过反射调用的方法引发的异常。如果产生这个问题,可以想象是我们反射使用的有问题。同样如果我们去写反射的程序,我们不应该去catch这个,而是让他直接

报错,表示代码就不应该这么写,出错然后去解决。

好吧,回到原问题上,假如没有找到兼容性的catch块那么会怎么样呢?

那么实验一下吧。

实验如下:

会抛出异常的,所以我们写代码的时候需要确保最后一个一定能捕获到异常的,如果sw.Write(new byte[1]);出现错误那么非托管资源就没有释放。

那么如何万一我们没有捕获到异常也能是否非托管资源呢?使用using。

static void Main(string[] args)

{

try

{

using (var sw = new FileStream(@"C:\test\test.txt", FileMode.Open, FileAccess.ReadWrite))

{

sw.Write(new byte[1]);

}

}

catch (DirectoryNotFoundException ex)

{

//文件目录没有找到异常

Console.WriteLine(ex);

}

finally

{

}

}

这样写无论是否异常,那么都会释放非托管资源。这个是可以实验的,我把我的实验贴一下。

上面的说明已经被关闭了,我们再次关闭的时候将会产生异常。这个using可以看下IL,就清楚其中的原理。

异常过滤器

异常过滤器是c#的东西,这里只是做简单的介绍,后面异常代码优化章节中会重点介绍。

public static bool ConsoleLogException(Exception e)

{

var oldColor = Console.ForegroundColor;

Console.ForegroundColor = ConsoleColor.Red;

Console.WriteLine("Error:{0}",e);

Console.ForegroundColor = oldColor;

return false;

}

static void Main(string[] args)

{

int i = 10;

try

{

throw new TimeoutException("time out");

}

catch (Exception e) when(ConsoleLogException(e))

{

}

catch (TimeoutException e) when (i == 10)

{

}

finally

{

Console.ReadKey();

}

}

输出结果:

上面说明一个问题,无论catch是否匹配,when都会执行。

同样来做一个实验,判断when 如果不匹配也就是没有catch块执行那么会怎么样?

static void Main(string[] args)

{

int i = 10;

try

{

throw new TimeoutException("time out");

}

catch (Exception e) when(ConsoleLogException(e))

{

}

catch (TimeoutException e) when (i == 9)

{

Console.WriteLine("enter timeoutexception");

}

finally

{

Console.ReadKey();

}

}

结果:

重新抛出异常

为什么要重新抛出异常呢?

static int GetValueFromArray(int[] array, int index)

{

try

{

return array[index];

}

catch (System.IndexOutOfRangeException ex)

{

System.ArgumentException argEx = new System.ArgumentException("Index is out of range", "index", ex);

throw argEx;

}

}

本来是要抛出IndexOutOfRangeException 改为了ArgumentException 这是为什么呢?

原因如下:用户要调用的是我们的方法GetValueFromArray,传入的是参数,方法没有越界这么一说,而不是一个数组,所以我们要争对我们的目的来确定我们抛出的异常。

用户自定义异常

自定义异常水比较深,在此只做一个简单的介绍,单独一节补齐。

class CostumExcetion:Exception

{

public CostumExcetion(string message) : base(message)

{

}

}

调用如下:

static void Main(string[] args)

{

int i = 10;

try

{

throw new CostumExcetion("自定义异常");

}

catch (Exception e) when(ConsoleLogException(e))

{

}

catch (CostumExcetion e) when (i == 10)

{

Console.WriteLine(e.Message);

}

finally

{

Console.ReadKey();

}

}

调用者信息

现在又一个需要,知道try中出现错误,但是我需要知道是那一会出现错误,这个怎么破呢?

也就是说我们希望定位到行级,那么我们就需要调用者信息了。

在异常中,我们又很多方法调用一个方法,然后在这个方法中出现问题,我们需要知道是怎么报错的,到底是哪个函数调用报错的,这时候我们需要使用查询到调用者信息。

举个例子:

public class CallerInformationHelper

{

public void Log([CallerLineNumber]int line = -1, [CallerFilePath] string path = null, [CallerMemberName]string name = null)

{

Console.WriteLine((line<0)?"no line":"Line"+line);

Console.WriteLine((path==null)?"No file path":path);

Console.WriteLine((name==null)?"No Member name":name);

Console.WriteLine();

}

}

调用:

CallerInformationHelper helper = new CallerInformationHelper();

helper.Log();

深圳网站建设www.sz886.com

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 什么是异常? 异常实际上是程序中错误导致中断了正常的指令流的一种事件. 异常处理的优点: 没有处理错误的程序: r...
    沉麟阅读 569评论 0 0
  • 支持Http、Tcp和Udp的类组成了TCP/IP三层模型(请求响应层、应用协议层、传输层)的中间层-应用协议层,...
    moshanghuakai88阅读 1,314评论 0 0
  • 1.静态的using声明 静态的using声明允许调用静态方法时不使用类名: // C# 5using Syst...
    单程车票_SJ阅读 365评论 0 1
  • 引言 我一直在探寻一个高性能的Socket客户端代码。以前,我使用Socket类写了一些基于传统异步编程模型的代码...
    编程小世界阅读 792评论 0 0
  • 一直用C#开发程序,.NET的功能越来越多,变化也挺大的,从最初的封闭,到现在的开源,功能不断的增加,一直在进步。...
    编程小世界阅读 295评论 0 1