Foreach语句,Throw语句,Try,catch语句的使用,报告编译错误
2013-07-12 13:22
471 查看
1、Foreach语句
2、Throw语句
3、try,catch语句
4、#deine ERROR\#define WARNING
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace Foreach语句的使用 { class Program { static void Main(string[] args) { Console.Title = "Foreach语句的使用"; Console.BackgroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.DarkCyan; Console.Clear(); Hashtable cityHash = new Hashtable(); cityHash.Add("010", "北京"); cityHash.Add("020", "上海"); cityHash.Add("030", "天津"); Console.WriteLine("电话区号\t城市"); foreach (string a in cityHash.Keys) { Console.WriteLine("{0}\t\t{1}", a, cityHash[a]); } Console.WriteLine(); Console.Read(); } } }
2、Throw语句
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Throw语句的使用 { class Program { static void Main(string[] args) { Console.Title = "Throw语句的使用"; Console.BackgroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.DarkGreen; Console.Clear(); string username = ""; try { Console.Write("请输入用户名:"); username = Console.ReadLine(); if (username == "") throw new ArgumentException("", "用户名不能为空!\n"); } catch(ArgumentException e) { Console.ForegroundColor=ConsoleColor.Red; Console.WriteLine(e.Message); } finally { Console.ForegroundColor = ConsoleColor.Cyan; if (username != "") { Console.WriteLine("{0}用户,您好!\n", username); } } } } }
3、try,catch语句
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Try_Catch语句的使用 { class Program { static void Main(string[] args) { int x, y, z = 0; Console.Title = "异常处理示例"; try { Console.Write("请输入一个整数(被除数):"); x = int.Parse(Console.ReadLine()); Console.Write("请输入另一个整数(除数):"); y = int.Parse(Console.ReadLine()); z = x / y; } catch(FormatException e) { Console.WriteLine("发生异常:{0}", e.GetType()); Console.WriteLine(e.Message); Console.WriteLine("应输入一个整数"); } catch (DivideByZeroException e) { Console.WriteLine("发生异常:{0}",e.GetType()); Console.WriteLine(e.Message); Console.WriteLine("除数不能为0"); } catch(OverflowException e) { Console.WriteLine("发生异常:{0}",e.GetType()); Console.WriteLine(e.Message); Console.WriteLine("输入的数字超出整数范围"); } finally { Console.WriteLine("商={0}",z); Console.WriteLine("谢谢使用!\n"); } } } }
4、#deine ERROR\#define WARNING
#define ERROR #define WARNING using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 报告编译错误 { class Program { static void Main(string[] args) { #if(ERROR&&WARNING) //#error #warning #endif Console.WriteLine("Hello,world!!\n"); } } }
相关文章推荐
- JavaScript_try...catch语句与throw结合使用示例
- 在ios开发中使用 try 和 catch 来捕获错误。
- 经验分享,如何使用try,catch, throw之一
- JavaScript错误_throw、try和catch
- return 于try{}catch(Exception e){}finally{}语句结合使用的一点注意事项
- SQL Server 2005 中能够使用 Try...Catch语句
- 在VC中使用switch语句时遇到“error C2360: initialization of 'k' is skipped by 'case' label”的编译错误。
- JavaScript基础1:JavaScript 错误 - Throw、Try 和 Catch
- C++中的异常捕获与处理:Try、Throw、Catch关键字的理解和使用
- 在ios开发中使用 try 和 catch 来捕获错误。
- JavaScript 错误处理, Throw、Try 和 Catch入门
- sql语句trycatch捕获错误信息
- Java异常处理之try...catch...语句的使用进阶
- JavaScript 错误 - Throw、Try 和 Catch
- zz: 如何使用try,catch, throw
- (转载)使用 TRY/CATCH 语句解决 SQL Server 2005 死锁
- try..catch..finally块中的跳转语句(return,break,continue)使用注意事项
- 编译错误:Try没有catch或者Finally
- 编译错误:无法在要求对象展开的函数中使用 __try
- JavaScript 错误 - Throw、Try 和 Catch