後藤エラーハンドラその手順の上部が良いプログラミングスタイルであり、ほとんどの手続きのエラー処理の最小量であるべきです。しかし、エラーを引き起こす可能性のあるコード行の後にエラーをチェックするよりも柔軟性がありません。非常に簡単な手順でエラー処理を追加する場合は、On Error GoTo ...ステートメントと、ルーチンの一番下にあるcatch-allエラーハンドラを使用します。
On Error GoTo procErrorHandler
If Not Exists(BaseDirectory + "\ARCHIVE") Then
MkDir BaseDirectory + "\ARCHIVE"
End If
intvalue1 = 12
intvalue2 = 0
intvalue3 = intvalue1/intvalue
ProcExit:
Exit Sub
procErrorHandler:
Call MsgBox("There was an error in the procedure. Error " & CStr(Err.Number) & ", " & Err.Description, vbExclamation, App.Title)
Resume ProcExit ' A chance to do any cleanup needed
それは読んで従って、コードを困難にしているので、私は、複数の後藤文のファンではありません。私がいくつかのステップを実行している途中で、コードがどこに間違っているのかを正確に説明するエラーを返すか、エラーから回復して続ける可能性がある状況で、キャッチオール重要なステップの後にErr.Numberプロパティをチェックしてください。私がMattのエラー処理を変更すると、私はこの方法でコードを作成します。
On Error Resume Next
If Not Exists(BaseDirectory + "\ARCHIVE") Then
MkDir BaseDirectory + "\ARCHIVE"
End If
' check for errors making the directory
If Err.Number <> 0 Then
Call MsgBox("Error making directory - " & Err.Number & vbCrLf & Err.Description, vbExclamation, App.Title)
End If
intvalue1 = 12
intvalue2 = 0
intvalue3 = intvalue1/intvalue
' check for errors getting intvalue3
If Err.Number <> 0 Then
Call MsgBox("Error doing arithmetic - " & Err.Number & vbCrLf & Err.Description, vbExclamation, App.Title)
End If
Exit Sub
出典
2012-01-27 18:02:03
jac
ありがとうございます! – JimDel
+1。ドキュメントはこれを非常にはっきりと説明しています... http://msdn.microsoft.com/en-us/library/aa266173(v=vs.60).aspx – MarkJ
間違っています - ゼロによる除算は、MkDir操作が行う場合にのみ処理されますエラーをスローしません –