Exceptions: Difference between revisions

Content deleted Content added
Line 3,081:
End Sub</lang>
 
=={{header|Visual BasicVBA}} for Applications==
 
For historical reasons, Exceptions are called 'Errors' in VBA and VB Classic.
Line 3,100:
<lang vba>Sub bar1()
'by convention, a simple handler
On Error GotoGoTo Catchcatch
fooXfoo1
MsgBox " No Error"
Exit Sub
catch:
Catch:
'handle all exceptions
MsgBox Err.Number & vbCrLf & Err.Description
Exit Sub
End Sub
 
Sub bar2()
'a more complex handler, illustrating some of the flexibility of VBA exception handling
onOn errorError gotoGoTo catch
100 fooXfoo1
200 fooYfoo2
'finally block may be placed anywhere: this is complexity for it's own sake:
gotoGoTo finally
 
catch:
If ifErl erl= 100 thenThen
' handle exception at first line: in this case, by ignoring it:
Resume resume nextNext
elseElse
Select Case select case errErr.nummberNumber
case Case vbObjectError + 1050
' handle exceptions of type 1050
case vbObjectError + 1051 MsgBox "type 1050"
Case vbObjectError ' handle exceptions of type+ 1051
' handle exceptions of type 1051
case else
' handle anyMsgBox "type of exception not handled by above catches or line numbers1051"
resume finally Case Else
' handle any type of exception not handled by above catches or line numbers
 
MsgBox Err.Number & vbCrLf & Err.Description
End Select
Resume finally
End If
finally:
'code here occurs whether or not there was an exception
'block may be placed anywhere
'by convention, often just a drop through to an Exit Sub, rather tnan a code block
GotoGoTo end_try:
 
end_try:
'by convention, often just a drop through from the catch block
exitEnd subSub</lang>
 
=={{header|zkl}}==