尝试/捕获的替代方案? [英] Alternative to try / catch?

查看:55
本文介绍了尝试/捕获的替代方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字符串转换为DateTime类型。用户可以用各种格式输入日期

,我不确定它是否是有效的日期。是否有可能

来检查字符串是否可以转换为DateTime类型而不使用try

/ catch块。我想的可能是像是命令或命令。但

似乎都不适用于此。任何建议都将不胜感激。


现在我有:

string s =" 01/05 / 2006" ;; //用户将输入数据,

//这可能是什么

无效,如22/22/2006

try

{

DateTime dt = DateTime.Parse(s);

MessageBox.Show(" Date Parse Ok!");

//。其他处理

}

catch(FormatException formatEx)

{

MessageBox .Show(formatEx.Message);

}

我在考虑一些事情:


if(s是DateTime)

MessageBox.Show(" Date !!!");

else

MessageBox.Show(" String .. 。$ ;;


但这不起作用,因为C#总是认为s是字符串类型。

有没有办法不使用试试/ catch块?我正在使用.Net ver

1.1


提前致谢。

解决方案

< blockquote> Tantr,


除了使用Try / Catch之外,没有办法。除非当然,否则你需要自己解析格式。
但是,我认为try / catch在可维护性方面更好。$ / b
当你转移到.NET 2.0时,你可以使用TryParse方法在

日期时间结构。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Tantr Mantr" < Ta ******** @ discussion.microsoft.com写信息

新闻:EF ******************** ************** @ microsof t.com ...


>我想将字符串转换为DateTime类型。用户可以用各种格式输入

的日期,我不确定它是否是有效的日期。是否可以

可以

检查字符串是否可以转换为DateTime类型而不使用

试试

/ catch块。我想的可能就像是或作为命令。



似乎都不适用于此。任何建议都会很多。

赞赏。


现在我有:

string s =" 01/05 / 2006" ;; //用户将输入数据,

//这可能是什么

无效,如22/22/2006

try

{

DateTime dt = DateTime.Parse(s);

MessageBox.Show(" Date Parse Ok!");

//。其他处理

}

catch(FormatException formatEx)

{

MessageBox .Show(formatEx.Message);

}


我在考虑一些事情:


if(s是DateTime)

MessageBox.Show(" Date !!!");

else

MessageBox.Show(" ; String ...");


但这不起作用,因为C#总是认为s是字符串类型。

有没有办法不使用try / catch块?我正在使用.Net ver

1.1


提前致谢。



非常感谢Nicholas!

我如何自己解析格式?我应该看看正则表达式吗?

只是因为在网格的列中DateTime是

,因此Try / catch使应用程序变得非常慢,所以称为多个时间。


干杯!


" Nicholas Paldino [.NET / C#MVP]"写道:


Tantr,


除了使用Try / Catch之外,没有办法。除非当然,否则你需要自己解析格式。
但是,我认为try / catch在可维护性方面更好。$ / b
当你转移到.NET 2.0时,你可以使用TryParse方法在

日期时间结构。


希望这会有所帮助。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Tantr Mantr" < Ta ******** @ discussion.microsoft.com写信息

新闻:EF ******************** ************** @ microsof t.com ...


我想将字符串转换为DateTime类型。用户可以用各种格式输入

的日期,我不确定它是否是有效的日期。是否可以

可以

检查字符串是否可以转换为DateTime类型而不使用

试试

/ catch块。我想的可能就像是或作为命令。



似乎都不适用于此。任何建议都会很多。

赞赏。


现在我有:

string s =" 01/05 / 2006" ;; //用户将输入数据,

//这可能是什么

无效,如22/22/2006

try

{

DateTime dt = DateTime.Parse(s);

MessageBox.Show(" Date Parse Ok!");

//。其他处理

}

catch(FormatException formatEx)

{

MessageBox .Show(formatEx.Message);

}

我在考虑一些事情:


if(s是DateTime)

MessageBox.Show(" Date !!!");

else

MessageBox.Show(" String .. 。$ ;;


但这不起作用,因为C#总是认为s是字符串类型。

有没有办法不使用试试/ catch块?我正在使用.Net ver

1.1


提前致谢。




如果try / catch让你失望,你可能正在运行你的代码

通过调试器(例如按F5)。


这里有关于异常和性能的好读物:

http://www.yoda.arachsys.com/csharp/exceptions.html


On Tue,2006年7月4日01:29:01 -0700,Tantr Mantr

< Ta ******* *@discussions.microsoft.comwrote:


>非常感谢Nicholas!
我如何自己解析格式?我应该看看正则表达式吗?
只是因为日期时间是网格列中的那个,因此Try / catch使应用程序变得非常慢,所以多次调用。

Nicholas Paldino [.NET / C#MVP]"写道:


> Tantr,

除了使用Try / Catch之外,没有办法。当然,除非您想要自己解析格式。但是,我认为try / catch在可维护性方面更好。

当你转移到.NET 2.0时,你可以在
DateTime结构上使用TryParse方法。

希望这会有所帮助。

-
- Nicholas Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse.com

Tantr Mantr < Ta ******** @ discussion.microsoft.com写信息
新闻:EF ************************ ********** @ microso ft.com ...


>我想将字符串转换为DateTime类型。用户可以用各种格式输入

的日期,我不确定它是否是有效的日期。是否可以

可以

检查字符串是否可以转换为DateTime类型而不使用

试试

/ catch块。我想的可能就像是或作为命令。



似乎都不适用于此。任何建议都会很多。

赞赏。


现在我有:

string s =" 01/05 / 2006" ;; //用户将输入数据,

//这可能是什么

无效,如22/22/2006

try

{

DateTime dt = DateTime.Parse(s);

MessageBox.Show(" Date Parse Ok!");

//。其他处理

}

catch(FormatException formatEx)

{

MessageBox .Show(formatEx.Message);

}


我在考虑一些事情:


if(s是DateTime)

MessageBox.Show(" Date !!!");

else

MessageBox.Show(" ; String ...");


但这不起作用,因为C#总是认为s是字符串类型。

有没有办法不使用try / catch块?我正在使用.Net ver

1.1


提前致谢。




I want to convert a string to a DateTime type. The user can enter the date in
various formats and I am not sure if it will be a valid Date. Is it possible
to check if a string can be converted to a DateTime type without using a try
/ catch block. I was thinking of maybe something like is or as commands. But
both do not seem to work for this. Any suggestions will be much appreciated.

Right now i have :
string s = "01/05/2006"; //user will be entering the data ,
//this could be something
invalid like 22/22/2006
try
{
DateTime dt = DateTime.Parse(s);
MessageBox.Show("Date Parse Ok!");
//..other processing
}
catch(FormatException formatEx)
{
MessageBox.Show(formatEx.Message);
}
I was thinking of something along the lines :

if(s is DateTime)
MessageBox.Show("Date !!!");
else
MessageBox.Show("String...");

but this does not work as C# always considers s to be of type string.
Is there any way of not using the try / catch block? I am using .Net ver
1.1

Thanks in advance.

解决方案

Tantr,

Other than using a Try/Catch, there isn''t a way. Unless of course, you
want to parse the format yourself. However, I think that the try/catch is
better in terms of maintainability.

When you move to .NET 2.0, you can use the TryParse method on the
DateTime structure.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tantr Mantr" <Ta********@discussions.microsoft.comwrote in message
news:EF**********************************@microsof t.com...

>I want to convert a string to a DateTime type. The user can enter the date
in
various formats and I am not sure if it will be a valid Date. Is it
possible
to check if a string can be converted to a DateTime type without using a
try
/ catch block. I was thinking of maybe something like is or as commands.
But
both do not seem to work for this. Any suggestions will be much
appreciated.

Right now i have :
string s = "01/05/2006"; //user will be entering the data ,
//this could be something
invalid like 22/22/2006
try
{
DateTime dt = DateTime.Parse(s);
MessageBox.Show("Date Parse Ok!");
//..other processing
}
catch(FormatException formatEx)
{
MessageBox.Show(formatEx.Message);
}
I was thinking of something along the lines :

if(s is DateTime)
MessageBox.Show("Date !!!");
else
MessageBox.Show("String...");

but this does not work as C# always considers s to be of type string.
Is there any way of not using the try / catch block? I am using .Net ver
1.1

Thanks in advance.



Many Thanks Nicholas!
How do I parse the Format myself? Should I be looking at Regex?
Its just that the Try/catch is making the app quite slow as the DateTime are
in a Column of a grid , so called multiple times.

Cheers!

"Nicholas Paldino [.NET/C# MVP]" wrote:

Tantr,

Other than using a Try/Catch, there isn''t a way. Unless of course, you
want to parse the format yourself. However, I think that the try/catch is
better in terms of maintainability.

When you move to .NET 2.0, you can use the TryParse method on the
DateTime structure.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tantr Mantr" <Ta********@discussions.microsoft.comwrote in message
news:EF**********************************@microsof t.com...

I want to convert a string to a DateTime type. The user can enter the date
in
various formats and I am not sure if it will be a valid Date. Is it
possible
to check if a string can be converted to a DateTime type without using a
try
/ catch block. I was thinking of maybe something like is or as commands.
But
both do not seem to work for this. Any suggestions will be much
appreciated.

Right now i have :
string s = "01/05/2006"; //user will be entering the data ,
//this could be something
invalid like 22/22/2006
try
{
DateTime dt = DateTime.Parse(s);
MessageBox.Show("Date Parse Ok!");
//..other processing
}
catch(FormatException formatEx)
{
MessageBox.Show(formatEx.Message);
}
I was thinking of something along the lines :

if(s is DateTime)
MessageBox.Show("Date !!!");
else
MessageBox.Show("String...");

but this does not work as C# always considers s to be of type string.
Is there any way of not using the try / catch block? I am using .Net ver
1.1

Thanks in advance.




If try/catch is slowing you down, you are probably running your code
through the debugger (pressing F5 for example).

Here''s a good read about exceptions and performance:

http://www.yoda.arachsys.com/csharp/exceptions.html


On Tue, 4 Jul 2006 01:29:01 -0700, Tantr Mantr
<Ta********@discussions.microsoft.comwrote:

>Many Thanks Nicholas!
How do I parse the Format myself? Should I be looking at Regex?
Its just that the Try/catch is making the app quite slow as the DateTime are
in a Column of a grid , so called multiple times.

Cheers!

"Nicholas Paldino [.NET/C# MVP]" wrote:

>Tantr,

Other than using a Try/Catch, there isn''t a way. Unless of course, you
want to parse the format yourself. However, I think that the try/catch is
better in terms of maintainability.

When you move to .NET 2.0, you can use the TryParse method on the
DateTime structure.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tantr Mantr" <Ta********@discussions.microsoft.comwrote in message
news:EF**********************************@microso ft.com...

>I want to convert a string to a DateTime type. The user can enter the date
in
various formats and I am not sure if it will be a valid Date. Is it
possible
to check if a string can be converted to a DateTime type without using a
try
/ catch block. I was thinking of maybe something like is or as commands.
But
both do not seem to work for this. Any suggestions will be much
appreciated.

Right now i have :
string s = "01/05/2006"; //user will be entering the data ,
//this could be something
invalid like 22/22/2006
try
{
DateTime dt = DateTime.Parse(s);
MessageBox.Show("Date Parse Ok!");
//..other processing
}
catch(FormatException formatEx)
{
MessageBox.Show(formatEx.Message);
}
I was thinking of something along the lines :

if(s is DateTime)
MessageBox.Show("Date !!!");
else
MessageBox.Show("String...");

but this does not work as C# always considers s to be of type string.
Is there any way of not using the try / catch block? I am using .Net ver
1.1

Thanks in advance.




这篇关于尝试/捕获的替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆