赞
踩
C#中的异常处理是一种检测和处理代码中运行时错误的机制,由try catch和finaly块提供支持。.NET框架提供了常见异常的内置类。程序执行期间发生的异常,它们可能是由于逻辑或系统错误引起的。如果程序员未提供处理这些异常的机制,则.NET运行时环境将提供默认机制,该机制将终止程序执行。
- try
- {
- //可能导致异常的语句
- }
- catch(Type x)
- {
- //处理异常的语句
- }
- finally
- {
- //任何清理代码
- }
- using System;
- class MyException
- {
- public static void Main()
- {
- int x = 0;
- int div = 100/x;
- Console.WriteLine(div);
- }
- }
具有异常处理机制的上述程序的修改形式如下:
在这里,我们使用标准异常类DivideByZeroException的对象来处理由除零引起的异常。
- using System;
- class MyException
- {
- public static void Main()
- {
- int x = 0;
- int div = 0;
- try
- {
- div = 100 / x;
- Console.WriteLine("此行未执行");
- }
- catch (DivideByZeroException)
- {
- Console.WriteLine("发生异常");
- }
- Console.WriteLine($"结果为 {div}");
- }
- }
以上代码的结果如下所示:
- using System;
- class MyException
- {
- public static void Main()
- {
- int x = 0;
- int div = 0;
- try
- {
- div = 100/x;
- Console.WriteLine("此行未执行");
- }
- catch(DivideByZeroException)
- {
- Console.WriteLine("发生异常");
- }
- finally
- {
- Console.WriteLine("Finally块");
- }
- Console.WriteLine($"结果为 {div}");
- }
- }
请记住,在C#中,catch块是可选的。以下程序在C#中是完全合法的。
- using System;
- class MyException
- {
- public static void Main()
- {
- int x = 0;
- int div = 0;
- try
- {
- div = 100/x;
- Console.WriteLine("此行未执行");
- }
- finally
- {
- Console.WriteLine("Finally块");
- }
- Console.WriteLine($"结果为 {div}");
- }
- }
- using System;
- class MyException
- {
- public static void Main()
- {
- int x = 0;
- int div = 0;
- try
- {
- div = 100 / x;
- Console.WriteLine("此行未执行");
- }
- catch (DivideByZeroException de)
- {
- Console.WriteLine("DivideByZeroException");
- }
- catch (Exception)
- {
- Console.WriteLine("Exception");
- }
- finally
- {
- Console.WriteLine("Finally块");
- }
- Console.WriteLine($"结果为 {div}");
- }
- }
- using System;
- class MyException
- {
- public static void Main()
- {
- int x = 0;
- int div = 0;
- try
- {
- div = 100 / x;
- Console.WriteLine("此行未执行");
- }
- catch
- {
- Console.WriteLine("oException");
- }
- Console.WriteLine($"结果为 {div}");
- }
- }
以下程序使用Exception对象处理所有异常。
- using System;
- class MyException
- {
- public static void Main()
- {
- int x = 0;
- int div = 0;
- try
- {
- div = 100 / x;
- Console.WriteLine("此行未执行");
- }
- catch (Exception)
- {
- Console.WriteLine("oException");
- }
- Console.WriteLine($"结果为 {div}");
- }
- }
throw exception_obj;
- throw new ArgumentException("Exception");
-
- using System;
- class MyException
- {
- public static void Main()
- {
- try
- {
- throw new DivideByZeroException("Invalid Division");
- }
- catch (DivideByZeroException)
- {
- Console.WriteLine("Exception");
- }
- Console.WriteLine("LAST STATEMENT");
- }
- }
- //C#: Exception Handling: Handling all exceptions
- using System;
- class MyClass
- {
- public void Method()
- {
- try
- {
- int x = 0;
- int sum = 100 / x;
- }
- catch (DivideByZeroException)
- {
- throw;
- }
- }
- }
- class MyException
- {
- public static void Main()
- {
- MyClass mc = new MyClass();
- try
- {
- mc.Method();
- }
- catch (Exception)
- {
- Console.WriteLine("此处捕获异常");
- }
- Console.WriteLine("LAST STATEMENT");
- }
- }
- using System;
-
- //用户自定义的异常
- class CustomException : Exception
- {
- public CustomException(string str)
- {
- Console.WriteLine("用户定义的异常");
- }
- }
-
- class MyException
- {
- public static void Main()
- {
- try
- {
- throw new CustomException("RAJESH");
- }
- catch (Exception e)
- {
- Console.WriteLine("此处捕获异常" + e.ToString());
- }
- Console.WriteLine("LAST STATEMENT");
- }
- }
代码如下:
- static void Main(string[] args)
- {
- Console.WriteLine(GetNum());
- }
-
- public static int GetNum()
- {
- int Num=1;
- try
- {
- Console.WriteLine("try");
- return Num;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- ++Num;
- Console.WriteLine("finally");
- }
- }
输出结果如下:
结论:try中的return语句先于finally中的函数执行所以,返回的结果是1, 而不是2。
从运行结果可以看出,return语句执行后,将把返回结果放置进函数栈中,此时函数并不是马上返回,它要执行finally语句后才真正开始返回。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。