C#如何使用 Try/Catch 块捕捉异常
来源:互联网 发布:xampp apache 编辑:程序博客网 时间:2024/05/20 20:43
下面的代码示例使用 Try/Catch 块捕捉可能的异常。Main 方法包含带有 StreamReader 语句的 Try 块,该语句打开名为 data.txt 的数据文件并从该文件写入字符串。Try 块后面是 Catch 块,该块捕捉 Try 块产生的任何异常。
此示例阐释捕捉任何异常的基本 Catch 语句。一般而言,好的编程做法是捕捉特定类型的异常而不是使用基本 Catch 语句。有关捕捉特定异常的更多信息,请参见在 Catch 块中使用特定异常。using System;using System.IO;using System.Security.Permissions;// Security permission request.[assembly:FileIOPermissionAttribute(SecurityAction.RequestMinimum, All = @"c:\data.txt")]public class ProcessFile { public static void Main() { try { StreamReader sr = File.OpenText("data.txt"); Console.WriteLine("The first line of this file is {0}", sr.ReadLine()); } catch(Exception e) { Console.WriteLine("An error occurred: '{0}'", e); } }}
- C#如何使用 Try/Catch 块捕捉异常
- 捕捉异常try{}catch{}
- 异常----异常捕捉try&catch
- try catch捕捉所有异常
- try-catch 捕捉不到异常
- try,catch,throw异常捕捉
- VB6 异常捕捉 Try catch
- C#中异常捕捉try catch效率问题
- C#如何在 Catch 块中使用特定异常
- php try() catch() : 捕捉异常,抛出异常
- [vc] try catch捕捉所有异常
- c++中的异常捕捉:try和catch
- java 异常捕捉 ( try catch finally )
- 047.@try…@catch... 捕捉异常
- try catch finally java 异常捕捉
- 047.@try…@catch... 捕捉异常
- java里面try,catch,finally异常捕捉
- c++中的异常捕捉try和catch
- 分页(一)
- Android学习笔记--四大组件之BroadcastReceiver
- 《partner4java 讲述MyBatis入门》之第一步:MyBatis HelloWorld
- jquery 操作滚动条滚动 animate checkbox操作
- linux sysinfo函数使用方法--获取内存大小
- C#如何使用 Try/Catch 块捕捉异常
- 尾递归
- someone like you
- 如何在eclipse中编写好程序,然后打包成jar文件,在hadoop分布式集群上运行
- C#WebService分析
- 做一个缩略图片(php)
- Evaluating Combiantions
- webservice 自动更新
- java IO体系结构