VB 循环错误处理
答案:5 悬赏:80
解决时间 2021-02-16 08:49
- 提问者网友:熱戀丶瘋
- 2021-02-16 04:56
从1循环到1000到中途出现错误怎么处理,还要继续后面没循环的。
最佳答案
- 二级知识专家网友:努力只為明天
- 2021-02-16 05:01
不需要处理的话就在循环的开头用
on error resume next
需要处理的话就用
On Error GoTo line
启动错误处理程序,且该例程从必要的 line 参数中指定的 line 开始。line 参数可以是任何行标签或行号。如果发生一个运行时错误,则控件会跳到 line,激活错误处理程序。指定的 line 必须在一个过程中,这个过程与 On Error 语句相同; 否则会发生编译时间错误。
on error resume next
需要处理的话就用
On Error GoTo line
启动错误处理程序,且该例程从必要的 line 参数中指定的 line 开始。line 参数可以是任何行标签或行号。如果发生一个运行时错误,则控件会跳到 line,激活错误处理程序。指定的 line 必须在一个过程中,这个过程与 On Error 语句相同; 否则会发生编译时间错误。
全部回答
- 1楼网友:抱不住太阳的深海
- 2021-02-16 08:36
用goto的可读性很差,而且,你只在开头 on error goto err_law_info_ceshi,跳一次就不再跳了.
你不如换种方法
on error resume next
err.clear
do.....
if err.number =0 then 没错
else 出错
end if 这样处理好些吧.
-------------------------以下为我帮你修改的代码,手写没有测试的,不知道能不能用.
private sub command8_click()
dim i as integer
for i = 0 to lst_file.listcount - 1
lst_file.listindex = i
dbpath = "dbq=" & lst_file.text
strname_xls = lst_file.text 'excel文件名
strsheetname_xls = "sheet2" 'excel表名
dim rtnstr as string
rtnstr = ""
dim cnstr as string
cnstr = "provider=microsoft.jet.oledb.4.0;data source=" & strname_xls & ";extended properties='excel 8.0;hdr=yes;imex=1'"
if tryconnexcel(cnnstr, strsheetname_xls, rtnstr) then
'这里是你的成功代码,
exit for '如果你要退出
else
'如果不成功你再处理别的吧.
lst_err.additem rtnstr & strname_xls
end if
next
msgbox "完成law_info"
end sub
function tryconnexcel(byval cnnstr as string, byval strsheetname_xls as string, byref strrtn as string) as boolean
dim conn_xls as new adodb.connection
dim rs_xls as new adodb.recordset
on error resume next
dim rtn as boolean
rtn = false
err.clear
conn_xls.open "provider=microsoft.jet.oledb.4.0;data source=" & filename & ";extended properties='excel 8.0;hdr=yes;imex=1'"
if err.number = 0 then
rs_xls.open "select * from [" & strsheetname_xls & "$]", conn_xls, adopenforwardonly, adlockoptimistic
low_name = rs_xls(0)
low_number = rs_xls(1)
low_info = rs_xls(2)
rs_xls.close
if err.number = 0 then
rtn = true
strrtn = ""
end if
end if
if err.number <> 0 then
strrtn = "(" & err.number & ")" & err.description & " "
end if
conn_xls.close
set conn_xls = nothing
set crs_xls = nothing
err.clear
tryconnexcel = rtn
end function
- 2楼网友:怪咖小青年
- 2021-02-16 07:42
有两种处理方法
1、抛出异常
2、判断是否遇到错误,用continue继续
- 3楼网友:两不相欠
- 2021-02-16 06:30
错误有两种,一种是编码错误导致的错误,你需要检查出错误的原因来修正,否则运行结果可能与实际结果不相同了;另一种是系统误差导致的错误,你用楼上的方法就可以了
- 4楼网友:啵啵桃汀
- 2021-02-16 05:07
继续的话,那么进行循环可以使用:
On Error Resume Next
'这里写代码
会避开错误,不影响程序执行;
如果不加的话那么遇到错误就会退出。
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯