主题:有关TRY块的问题
代码段一:
while ( true ){
str = sr.ReadLine();
if ( str == null )
{
break;
}
//try {
str = str.Substring(0,5) + " " + str.Substring (6,1)
str += str.Substring(8,1) + str.Substring (10,1) ;
total++;
listbox.Items.Add( " " + total.ToString().PadLeft(3) + " " + str );
//}
//catch{
//}
} // end of while
代码段二:
while ( true ){
str = sr.ReadLine();
if ( str == null )
{
break;
}
try {
str = str.Substring(0,5) + " " + str.Substring (6,1)
str += str.Substring(8,1) + str.Substring (10,1) ;
total++;
listbox.Items.Add( " " + total.ToString().PadLeft(3) + " " + str );
}
catch{
}
} // end of while
代码段一和二的唯一不同之处就在于代码二有一TRY块.
问题: 以上两段代码在.NET 环境中运行均OK,结果也一致.但编译成EXE文件后,代码段
二运行正常,但代码段一运行到
str = str.Substring(0,5) + " " + str.Substring (6,1)
语句时便出现运行时错误,说是STRING转换函数超界.
这两段代码运行环境一样,我可以肯定不存在超界的情况,这是怎么回事?
while ( true ){
str = sr.ReadLine();
if ( str == null )
{
break;
}
//try {
str = str.Substring(0,5) + " " + str.Substring (6,1)
str += str.Substring(8,1) + str.Substring (10,1) ;
total++;
listbox.Items.Add( " " + total.ToString().PadLeft(3) + " " + str );
//}
//catch{
//}
} // end of while
代码段二:
while ( true ){
str = sr.ReadLine();
if ( str == null )
{
break;
}
try {
str = str.Substring(0,5) + " " + str.Substring (6,1)
str += str.Substring(8,1) + str.Substring (10,1) ;
total++;
listbox.Items.Add( " " + total.ToString().PadLeft(3) + " " + str );
}
catch{
}
} // end of while
代码段一和二的唯一不同之处就在于代码二有一TRY块.
问题: 以上两段代码在.NET 环境中运行均OK,结果也一致.但编译成EXE文件后,代码段
二运行正常,但代码段一运行到
str = str.Substring(0,5) + " " + str.Substring (6,1)
语句时便出现运行时错误,说是STRING转换函数超界.
这两段代码运行环境一样,我可以肯定不存在超界的情况,这是怎么回事?