如果异常就忽略它,怎么写?

tonglife 2004-01-18 07:07:44
这样一句话:
for () {
Bitmap myImage = new Bitmap(sFilePath);
}
其中有很多BMP文件是坏的,会报异常
我希望如果异常,就退出这次循环,删掉这个坏图像,再开始for,怎么作?
...全文
119 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
desrie 2004-01-18
  • 打赏
  • 举报
回复
所以我问得搂主是否有必要,程式更是思路问题
ssunday 2004-01-18
  • 打赏
  • 举报
回复
但是楼主的意思就是需要一旦有异常就删除文件并重新循环啊,不这样还可以怎样?
desrie 2004-01-18
  • 打赏
  • 举报
回复
for(i=0;i<count;i++)
{
try
{
Bitmap myImage = new Bitmap(sBitmapFilePath[i]);
}
catch
{
deletefile(sBitmapFilePath[i];
}
}

这个其实也可
ssunday 2004-01-18
  • 打赏
  • 举报
回复
也可以用while循环代替我刚才说的递归算法:

boolean HasError = true;
while(HasError)
{
try
{
for () {
Bitmap myImage = new Bitmap(sFilePath);}
HasError = false;
}
catch
{
//这里写些代码用于删除有问题的Bitmap;
}
desrie 2004-01-18
  • 打赏
  • 举报
回复
private void scanBitmaps()
{
try
{
for () {
Bitmap myImage = new Bitmap(sFilePath);}
}
catch
{
//这里写些代码用于删除有问题的Bitmap;
scanBitmaps();
}
}


会new 出来好多同样的Bitmap(sFilePath),
是否必要?
jimh 2004-01-18
  • 打赏
  • 举报
回复
for(i=0;i<count;i++)
{
try
{
Bitmap myImage = new Bitmap(sBitmapFilePath[i]);
}
catch {}
}
八爪鱼-杭州 2004-01-18
  • 打赏
  • 举报
回复
大家速度好快啊,才转眼就贴了这么多
八爪鱼-杭州 2004-01-18
  • 打赏
  • 举报
回复
以下代码没经测试
class testClass
{
private string[5] strBitmapFilePath=new string[5] {"c:\1.bmp","c:\2.bmp","c:\2.bmp","c:\2.bmp","c:\2.bmp"}
public void testFor(int count)
{
int i,j;
try
{
for(i=0;i<count;i++)
Bitmap myImage = new Bitmap(sBitmapFilePath[i]);
}
catch
{
for(int j=count-1;j>i;j++)
strBitmapFilePath[j-1]= strBitmapFilePath[j];
testFor(count-1);

}
}
}
ssunday 2004-01-18
  • 打赏
  • 举报
回复
不会啊,如果没有异常,就不会再递归调用,所以若全部文件没问题递归就会结束
desrie 2004-01-18
  • 打赏
  • 举报
回复
这样递归不循环死?
ssunday 2004-01-18
  • 打赏
  • 举报
回复
最后那个递归调用漏写了一个s,呵呵
ssunday 2004-01-18
  • 打赏
  • 举报
回复
用递归调用的方法:
private void scanBitmaps()
{
try
{
for () {
Bitmap myImage = new Bitmap(sFilePath);}
}
catch
{
//这里写些代码用于删除有问题的Bitmap;
scanBitmap();
}
}
desrie 2004-01-18
  • 打赏
  • 举报
回复
for ()
{
if (!this.checkfile(sFilePath))
deletefile();
}

private bool checkfile(string sFilePath)
{
try
{
Bitmap myImage = new Bitmap(sFilePath);
return true;
}
catch
{
return false;
}
}
cg1120II 2004-01-18
  • 打赏
  • 举报
回复
你可以在异常处理内回收
tonglife 2004-01-18
  • 打赏
  • 举报
回复
不行啊!这样就退出FOR循环了!我希望从新开始FOR循环,并且删除这个坏图像!!
cg1120II 2004-01-18
  • 打赏
  • 举报
回复
对,给出一个空的异常处理过程就可以了
八爪鱼-杭州 2004-01-18
  • 打赏
  • 举报
回复
try
{
for () {
Bitmap myImage = new Bitmap(sFilePath);}
}
catch
{
}

catch没东西就行了

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧