C#错误和异常处理典型例子
2010-06-28 16:04
218 查看
检查用户输入的是否是一个0-5中间的数字: 多重catch块
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
当输入的是非0-5之间的数字的时候,抛出的第一个异常,如果是一个字符串,抛出的第二个异常。第三个异常不带参数,这个catch块处理的是其他没有用C#编程的代码。
下面这个是MSDN一个try…finally的异常处理
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ExceptionDemo { class Program { static void Main(string[] args) { string userInput; while (true) { try { Console.Write("Input a number between 0 and 5" + "(or just hit return to exist)>"); userInput = Console.ReadLine(); if (userInput == "") break; int index = Convert.ToInt32(userInput); if (index < 0 || index > 5) throw new IndexOutOfRangeException("You typed in " + userInput); Console.WriteLine("Your number was " + index); } catch (IndexOutOfRangeException ex) { Console.WriteLine("Exception: " + "Number should between 0 and 5." + ex.Message); } catch (Exception ex) { Console.WriteLine("An exception was thrown. Message was: {0}" + ex.Message); } catch { Console.WriteLine("Some other exception has occured"); } finally { Console.WriteLine("Thank you"); } } } } }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
当输入的是非0-5之间的数字的时候,抛出的第一个异常,如果是一个字符串,抛出的第二个异常。第三个异常不带参数,这个catch块处理的是其他没有用C#编程的代码。
下面这个是MSDN一个try…finally的异常处理
static void CodeWithCleanup() { System.IO.FileStream file = null; System.IO.FileInfo fileInfo = null; try { fileInfo = new System.IO.FileInfo("C:\\file.txt"); file = fileInfo.OpenWrite(); file.WriteByte(0xF); } catch (System.Exception e) { System.Console.WriteLine(e.Message); } finally { if (file != null) { file.Close(); } } }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
相关文章推荐
- c# 错误和异常处理
- c# 错误和异常处理
- 经常会遇见java.lang.NullPointerExcetion 的错误,典型的一个错误例子,供大家参考
- 小议C#错误调试和异常处理
- 高级交叉报表例子程序(C#)中明细列统计数据错误改正!
- c#的返回值错误处理与异常处理
- 总结c++内存错误的典型情况
- C#遍历某目录及其子目录下所有文件的一个例子
- 利用C++编译器查找C程序中很隐蔽的错误的一个例子
- 豆豆与黄金剑, 一道典型DP题的C#解法
- MySQL存储过程中的错误处理 以及mybatis调用存储过程的例子
- FTA(fault tree analysis)的一个错误例子和FTA的应用环境
- Python中错误和异常处理
- C# 导出Word报”无法打开Office open xml文件。因为文件内容有错误“ 解决方法
- C# FTP,GetResponse(),远程服务器返回错误:(550) 文件不可用(例如,未找到文件,无法访问文件)
- 再谈PHP错误与异常处理
- C#调用存储过程简单完整例子
- c#中子线程控制进度条的一个简单例子
- C# 多态性例子
- 的C#事件例子