1.C#编程学习——关于try和catch使用
2016-11-21 23:04
316 查看
1.C#编程学习——关于try和catch使用
//-----------------------------------------------
// CsDateProperties.cs ?2001 byCharles Petzold
//-----------------------------------------------
usingSystem;
classCsDateProperties
{
publicstaticvoid Main()
{
Date mydate =
newDate();
try
{
mydate.Month = 8;
mydate.Day = 29;
mydate.Year = 2001;
Console.WriteLine("Day of year = {0}", mydate.DayOfYear);
}
catch (Exceptionexc)
{
Console.WriteLine(exc);
}
}
}
classDate
{
// Fields
int year;
int month;
int day;
staticint[] MonthDays =
newint[]{ 0, 31, 59, 90, 120, 151,
181, 212, 243, 273, 304, 334 };
// Properties
publicint Year
{
set
{
if (value < 1600)
thrownewArgumentOutOfRangeException("Year");
else
year =
value;
}
get
{
return year;
}
}
publicint Month
{
set
{
if (value < 1 ||
value > 12)
thrownewArgumentOutOfRangeException("Month");
else
month =
value;
}
get
{
return month;
}
}
publicint Day
{
set
{
if (value < 1 ||
value > 31)
thrownewArgumentOutOfRangeException("Day");
else
day =
value;
}
get
{
return day;
}
}
publicint DayOfYear
{
get
{
return MonthDays[month - 1] + day +
(month > 2&& IsLeapYear(year) ? 1 : 0);
}
}
// Method
publicstaticbool IsLeapYear(int
year)
{
return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
}
}
其中代码中使用
Date mydate =
newDate();来构造Date对象。
源代码
如下://-----------------------------------------------
// CsDateProperties.cs ?2001 byCharles Petzold
//-----------------------------------------------
usingSystem;
classCsDateProperties
{
publicstaticvoid Main()
{
Date mydate =
newDate();
try
{
mydate.Month = 8;
mydate.Day = 29;
mydate.Year = 2001;
Console.WriteLine("Day of year = {0}", mydate.DayOfYear);
}
catch (Exceptionexc)
{
Console.WriteLine(exc);
}
}
}
classDate
{
// Fields
int year;
int month;
int day;
staticint[] MonthDays =
newint[]{ 0, 31, 59, 90, 120, 151,
181, 212, 243, 273, 304, 334 };
// Properties
publicint Year
{
set
{
if (value < 1600)
thrownewArgumentOutOfRangeException("Year");
else
year =
value;
}
get
{
return year;
}
}
publicint Month
{
set
{
if (value < 1 ||
value > 12)
thrownewArgumentOutOfRangeException("Month");
else
month =
value;
}
get
{
return month;
}
}
publicint Day
{
set
{
if (value < 1 ||
value > 31)
thrownewArgumentOutOfRangeException("Day");
else
day =
value;
}
get
{
return day;
}
}
publicint DayOfYear
{
get
{
return MonthDays[month - 1] + day +
(month > 2&& IsLeapYear(year) ? 1 : 0);
}
}
// Method
publicstaticbool IsLeapYear(int
year)
{
return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
}
}
其中代码中使用
Date mydate =
newDate();来构造Date对象。
相关文章推荐
- 在进行C#编程时候,有的时候我们需要判断一个字符串是否是数字字符串,我们可以通过以下两种方法来实现。 【方法一】:使用 try{} catch{} 语句。 我们可以在try语句块中试图
- 关于.NET中try..catch的使用
- vc中关于try/catch结构的使用
- 关于try ...catch .. finally的使用
- c#关于try catch finally的使用,判断日期格式是否正确
- C#学习使用try-catch-finally错误处理表达式(续)--定义自己的异常消息类
- 并发编程学习笔记 三------- 关于在if中和while中使用wait(),唤醒之后的区别
- 关于linux入门的使用和编程的学习
- 最新关于try ,catch,throw的使用小结
- 学习Scala:使用try-catch表达式处理异常
- java关于异常处理只使用try-finally而不使用catch
- 使用 TRY/CATCH 语句解决 SQL Server 2005 死锁
- 学习如何使用 Cookie 编程
- 现在想起来,一个最早学习编程的时候的问题, 关于文本文件的读写
- 正在学习关于sniffer的编程,收集到的资料(有经过修正程序)
- kingofark关于学习C++和编程的50个观点
- 关于try...catch...finaly机制
- Kingofark关于学习C++和编程的50个观点2003修订版
- 今天使用sQL2005中Try_catch,以及分布式使用远程服务的函数
- 关于2.0 中的用户控件编程使用。