为什么IndexOf仍然返回-1 [英] why does IndexOf still return -1
问题描述
如果IndexOf只是扔了一个未找到的角色,那是不是有意义
异常而不是-1?
如果-1是你的话期待应该有一个函数ContainsChar
返回bool。
想法?
对于相同的输入数据,已知异常和基于已知回报的
之间有什么区别?你处理错误的逻辑很可能是相同的......
个人,只要我知道当我给出一个特定的时候会发生什么br />
运行一些数据,我不在乎它如何处理这样的事情。它可以返回-3.14159或者扔掉一个YoureAFoolForTryingThatException,就像它在文档中一样长。
Gordon Cowie写道:如果IndexOf只是扔了一个没有找到的字符而不是-1而不是-1这是不是有道理?
如果-1是你所期待的那样应该是一个函数ContainsChar
返回bool。
想法?
jeremiah johnson写道:在同一输入数据上,已知异常与已知返回之间的区别是什么?你处理错误的逻辑非常可能是相同的......
个人,只要我知道当我给出一个特定的
函数时会发生什么数据,我不在乎它如何处理这样的事情。它可以返回-3.14159或者抛出一个YoureAFoolForTryingThatException,就像它在文档中一样。
它将是更清洁的。至于分离错误处理类型的
代码。举个例如这个函数。
int a = SomeString.IndexOf(" f");
if(a!= -1)
{
int b = SomeString.IndexOf(" g");
if(b!= -1)
{
int c = SomeString.Substring(a,b).IndexOf(" i");
if(c!= -1)
{
return(SomeString.Substring(0,c);
}
else
抛出InvalidStuff;
}
else
抛出InvalidStuff;
}
否则
抛出InvalidStuff;
phew。现在有例外..
试试
{
返回Somestring.Substring(0,
SomeString.Substring(
SomeString.IndexOf(" f"),
SomeString.IndexOf(" g"))。IndexOf(" i"));
}
catch(CharNotFoundException e)
{
抛出InvalidStuff;
}
清洁没有?没有讨厌的a,b,c变量要么。
它将是更清洁的。至于错误处理类型的代码的分离。
但如果字符串不包含
指定字符,则不一定是错误。
尝试
{
返回Somestring.Substring(0,
SomeString.Substring(
SomeString.IndexOf(" f")),
SomeString.IndexOf( " g"))。IndexOf(" i));
}
catch(CharNotFoundException e)
{
抛出InvalidStuff;
}
你仍然可以这样做,如果你结束的话,子串shouold抛出
将-1传递给它。
Mattias
-
Mattias Sj?gren [C#MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
请回复到新闻组。
Wouldn''t it make sense if IndexOf just threw a character not found
exception instead of -1?
If -1 is what you were expecting there should be a function ContainsChar
that returns bool.
Thoughts?
what''s the difference between a known exception and a known return based
on the same input data? your logic to handle the error would very
likely be the same...
personally, as long as i know what happens when i give a particular
function some data, i don''t care how it handles something like that. it
could return -3.14159 or throw a YoureAFoolForTryingThatException, as
long as its in the documentation.
Gordon Cowie wrote:Wouldn''t it make sense if IndexOf just threw a character not found
exception instead of -1?
If -1 is what you were expecting there should be a function ContainsChar
that returns bool.
Thoughts?
jeremiah johnson wrote:what''s the difference between a known exception and a known return based
on the same input data? your logic to handle the error would very
likely be the same...
personally, as long as i know what happens when i give a particular
function some data, i don''t care how it handles something like that. it
could return -3.14159 or throw a YoureAFoolForTryingThatException, as
long as its in the documentation.
It would be "cleaner" as far as separation of the error-handling type of
code goes. Take, for example this function.
int a = SomeString.IndexOf("f");
if (a != -1)
{
int b = SomeString.IndexOf("g");
if (b != -1)
{
int c = SomeString.Substring(a,b).IndexOf("i");
if (c != -1)
{
return(SomeString.Substring(0,c);
}
else
throw InvalidStuff;
}
else
throw InvalidStuff;
}
else
throw InvalidStuff;
phew. now, with exceptions..
try
{
return Somestring.Substring(0,
SomeString.Substring(
SomeString.IndexOf("f"),
SomeString.IndexOf("g")).IndexOf("i"));
}
catch(CharNotFoundException e)
{
throw InvalidStuff;
}
cleaner no? no pesky a,b,c variables either.
It would be "cleaner" as far as separation of the error-handling type of
code goes.
But it''s not necessarily an error if the string doesn''t contain the
specified character.
try
{
return Somestring.Substring(0,
SomeString.Substring(
SomeString.IndexOf("f"),
SomeString.IndexOf("g")).IndexOf("i"));
}
catch(CharNotFoundException e)
{
throw InvalidStuff;
}
You can still do it this way, Substring shouold throw if you end up
passing -1 to it.
Mattias
--
Mattias Sj?gren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
这篇关于为什么IndexOf仍然返回-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!