我的c#之路(8.异常处理语句)
2014-06-11 21:28
267 查看
异常处理语句
1.
--在应用程序遇到异常情况(如被零除情况或内存不足警告)时,就会产生异常。
--在可能引发异常的语句周围使用try块。
--try块发生异常后,控制流会立即跳转到关联的异常处理程序(如果存在)。
--如果给定异常没有异常处理程序,则程序将停止执行,并显示一条错误消息。
--如果catch块定义了一个异常变量,则可以使用它来获取有关所发生异常的类型的更多信息。
--可能导致异常的操作通过try关键字来执行。
--异常处理程序是在异常发生时执行的代码块,在C#中,catch关键字用于定义异常处理程序。
--程序可以使用throw关键字显式地引发异常。
--异常对象包含有关错误的详细信息,其中包括调用堆栈的状态以及有关错误的文本说明。
--即使引发了异常,finally块中的代码也会执行,从而使程序可以释放资源。
2.
throw
--用于发出在程序执行期间出现反常情况(异常)的信号
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class ThrowTest
{
static void Main(string[] args)
{
string s = null;
if (s == null)
{
throw new ArgumentNullException();//使用throw语句引发异常
}
Console.Write("The string s is null");//not executed
}
}
}
3.
try-catch
--由一个try块后跟一个或多个catch子句构成,这些子句指定不同的异常处理程序
--try块包含可能导致异常的保护代码。该块一直执行到引发异常或成功完成为止
--catch子句使用时可以不带任何参数,这种情况下它捕获任何类型的异常
catch()
{
}
--catch子句可以接受从System.Exception派生的对象参数,这种情况下它处理特定的异常。
catch(InvalidCastException e)
{
}
--在同一个try-catch语句中可以使用一个以上的特定catch子句。这种情况下catch子句的顺序很重要,因为会按顺序检查catch子句。将先捕获特定程序较高的异常,而不是特定程度较小的异常。
catch(ArgumentNullException e)
{
Console.WriteLine("{0} First exception caught.",e);
}
catch(Exception e)
{
Console.WriteLine("{0} Second exception caught.",e);
}
--在catch块中可以使用throw语句再次引发已由catch语句捕获的异常
catch(InvalidCastException e)
{
throw(e);//Rethrowing exception e
}
或
catch
{
throw;
}
--在try块内部时应该只初始化其中声明的变量,否则,完成该块的执行前可能发生异常
int x;
try
{
x=123;//Error
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class ThrowTest
{
static void Main(string[] args)
{
string s = null;
if (s == null)
{
throw new ArgumentNullException();//使用throw语句引发异常
}
Console.Write("The string s is null");//not executed
}
}
}
使用了两个catch语句。最先出现的最特定的异常被捕获
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class MainClass
{
static void ProcessString(string s)
{
if (s == null)
{
throw new ArgumentNullException();//若要捕获特定程度最小的异常,使用throw new Exception();
}
}
static void Main(string[] args)
{
try
{
string s = null;
ProcessString(s);
}
//Most specific:
catch (ArgumentNullException e)
{
Console.WriteLine("{0} First exception caught.",e);
}
//Least specific:
catch (Exception e)
{
Console.WriteLine("{0} Second exception caught.", e);
}
}
}
}4.
try-finally
--finally块用于清除try块中分配的任何资源,以及运行任何即使在发生异常时也必须执行的代码。控制总是传递给finally块,与try块的退出方式无关。(catch用于处理语句块中出现的异常,而finally用于保证代码语句块的执行,与前面的try块的退出方式无关)
有一个导致异常的无效转换语句。当运行程序时,您收到一条运行时错误信息,但finally子句仍继续执行并显示输出。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class MainClass
{
static void Main(string[] args)
{
int i = 123;
string s = "Some string";
object o = s;
try
{
//Invalid conversion;o contains a string not an int
i = (int)o;
}
finally
{
Console.Write("i={0}",i);
}
}
}
}5.
try-catch-finally
--catch和finally一起使用的常见方式是:在try块中获取并使用资源,在catch块中处理异常情况,并在finally块中释放资源。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class MainClass
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Executing the try statement.");
throw new NullReferenceException();
}
catch (NullReferenceException e)
{
Console.WriteLine("{0} Caught exception #1.", e);
}
catch
{
Console.WriteLine("Caught exception #2.");
}
finally
{
Console.WriteLine("Executing finally block.");
}
}
}
}
1.
--在应用程序遇到异常情况(如被零除情况或内存不足警告)时,就会产生异常。
--在可能引发异常的语句周围使用try块。
--try块发生异常后,控制流会立即跳转到关联的异常处理程序(如果存在)。
--如果给定异常没有异常处理程序,则程序将停止执行,并显示一条错误消息。
--如果catch块定义了一个异常变量,则可以使用它来获取有关所发生异常的类型的更多信息。
--可能导致异常的操作通过try关键字来执行。
--异常处理程序是在异常发生时执行的代码块,在C#中,catch关键字用于定义异常处理程序。
--程序可以使用throw关键字显式地引发异常。
--异常对象包含有关错误的详细信息,其中包括调用堆栈的状态以及有关错误的文本说明。
--即使引发了异常,finally块中的代码也会执行,从而使程序可以释放资源。
2.
throw
--用于发出在程序执行期间出现反常情况(异常)的信号
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class ThrowTest
{
static void Main(string[] args)
{
string s = null;
if (s == null)
{
throw new ArgumentNullException();//使用throw语句引发异常
}
Console.Write("The string s is null");//not executed
}
}
}
3.
try-catch
--由一个try块后跟一个或多个catch子句构成,这些子句指定不同的异常处理程序
--try块包含可能导致异常的保护代码。该块一直执行到引发异常或成功完成为止
--catch子句使用时可以不带任何参数,这种情况下它捕获任何类型的异常
catch()
{
}
--catch子句可以接受从System.Exception派生的对象参数,这种情况下它处理特定的异常。
catch(InvalidCastException e)
{
}
--在同一个try-catch语句中可以使用一个以上的特定catch子句。这种情况下catch子句的顺序很重要,因为会按顺序检查catch子句。将先捕获特定程序较高的异常,而不是特定程度较小的异常。
catch(ArgumentNullException e)
{
Console.WriteLine("{0} First exception caught.",e);
}
catch(Exception e)
{
Console.WriteLine("{0} Second exception caught.",e);
}
--在catch块中可以使用throw语句再次引发已由catch语句捕获的异常
catch(InvalidCastException e)
{
throw(e);//Rethrowing exception e
}
或
catch
{
throw;
}
--在try块内部时应该只初始化其中声明的变量,否则,完成该块的执行前可能发生异常
int x;
try
{
x=123;//Error
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class ThrowTest
{
static void Main(string[] args)
{
string s = null;
if (s == null)
{
throw new ArgumentNullException();//使用throw语句引发异常
}
Console.Write("The string s is null");//not executed
}
}
}
使用了两个catch语句。最先出现的最特定的异常被捕获
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class MainClass
{
static void ProcessString(string s)
{
if (s == null)
{
throw new ArgumentNullException();//若要捕获特定程度最小的异常,使用throw new Exception();
}
}
static void Main(string[] args)
{
try
{
string s = null;
ProcessString(s);
}
//Most specific:
catch (ArgumentNullException e)
{
Console.WriteLine("{0} First exception caught.",e);
}
//Least specific:
catch (Exception e)
{
Console.WriteLine("{0} Second exception caught.", e);
}
}
}
}4.
try-finally
--finally块用于清除try块中分配的任何资源,以及运行任何即使在发生异常时也必须执行的代码。控制总是传递给finally块,与try块的退出方式无关。(catch用于处理语句块中出现的异常,而finally用于保证代码语句块的执行,与前面的try块的退出方式无关)
有一个导致异常的无效转换语句。当运行程序时,您收到一条运行时错误信息,但finally子句仍继续执行并显示输出。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class MainClass
{
static void Main(string[] args)
{
int i = 123;
string s = "Some string";
object o = s;
try
{
//Invalid conversion;o contains a string not an int
i = (int)o;
}
finally
{
Console.Write("i={0}",i);
}
}
}
}5.
try-catch-finally
--catch和finally一起使用的常见方式是:在try块中获取并使用资源,在catch块中处理异常情况,并在finally块中释放资源。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class MainClass
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Executing the try statement.");
throw new NullReferenceException();
}
catch (NullReferenceException e)
{
Console.WriteLine("{0} Caught exception #1.", e);
}
catch
{
Console.WriteLine("Caught exception #2.");
}
finally
{
Console.WriteLine("Executing finally block.");
}
}
}
}
相关文章推荐
- C#异常在catch语句中默认处理
- C#中异常处理语句Finally
- 黑马程序员----C#中的流程控制语句之异常处理语句
- 学习笔记之C#类、循环中的语句、基础知识和异常处理
- C#的throw异常处理语句
- c#描述异常处理语句try、catch、finally执行时的相互关系
- c#描述异常处理语句try、catch、finally执行时的相互关系
- C#2继承、foreach语句及异常处理语句
- C# 语句异常处理语句
- c#描述异常处理语句try、catch、finally执行时的相互关系
- c# 异常处理
- C#:Web Service异常处理
- C++、Java和C#中的异常处理
- C#读取WORD时发生“拒绝访问”及“消息筛选器显示应用程序正在使用中”异常的处理
- c#中构建异常处理
- 在C#中利用Keep-Alive处理Socket网络异常断开的方法
- [ASP.NET学习笔记之二十六]C#的异常处理
- C#中的异常处理
- 读书笔记_C#技术内幕_第十章(处理异常和错误)
- 在C#中利用Keep-Alive处理Socket网络异常断开的方法