Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

This answer is intended to simplify the Try/Catch pattern to be easily understandable.

This is not very different from regular inline error handling except that it can skip multiple lines at once, handle an error and then resume regular execution. This is a very cleanly structured pattern for handling an error. The flow moves very cleanly from top to bottom; no spaghetti code here.

Traditionally the error handler is placed at the bottom. But the Try/Catch construct is so elegent. It's a very structured way of handling errors and is very easy to follow. This pattern attempts to reproduce that in a very clean concise way. The flow is very consistent and doesn't jump from place to place.

Sub InLineErrorHandling()
 'code without error handling
BeginTry1:
 'activate inline error handler
 On Error GoTo ErrHandler1
 'code block that may result in an error
 Dim a As String: a = "Abc"
 Dim c As Integer: c = a 'type mismatch
ErrHandler1:
 'handle the error
 If Err.Number <> 0 Then
 'the error handler is now active
 Debug.Print (Err.Description)
 End If
 'disable previous error handler (VERY IMPORTANT)
 On Error GoTo 0
 'exit the error handler
 Resume EndTry1
EndTry1:
 'more code with or without error handling
End Sub

Sources:

Properly managed this works quite nicely. It is a very clean flowing pattern that is reproducible anywhere it is needed.

This answer is intended to simplify the Try/Catch pattern to be easily understandable.

This is not very different from regular inline error handling except that it can skip multiple lines at once, handle an error and then resume regular execution. This is a very cleanly structured pattern for handling an error. The flow moves very cleanly from top to bottom; no spaghetti code here.

Traditionally the error handler is placed at the bottom. But the Try/Catch construct is so elegent. It's a very structured way of handling errors and is very easy to follow. This pattern attempts to reproduce that in a very clean concise way. The flow is very consistent and doesn't jump from place to place.

Sub InLineErrorHandling()
 'code without error handling
BeginTry1:
 'activate inline error handler
 On Error GoTo ErrHandler1
 'code block that may result in an error
 Dim a As String: a = "Abc"
 Dim c As Integer: c = a 'type mismatch
ErrHandler1:
 'handle the error
 If Err.Number <> 0 Then
 'the error handler is now active
 Debug.Print (Err.Description)
 End If
 'disable previous error handler (VERY IMPORTANT)
 On Error GoTo 0
 'exit the error handler
 Resume EndTry1
EndTry1:
 'more code with or without error handling
End Sub

Sources:

Properly managed this works quite nicely. It is a very clean flowing pattern that is reproducible anywhere it is needed.

This answer is intended to simplify the Try/Catch pattern to be easily understandable.

This is not very different from regular inline error handling except that it can skip multiple lines at once, handle an error and then resume regular execution. This is a very cleanly structured pattern for handling an error. The flow moves very cleanly from top to bottom; no spaghetti code here.

Traditionally the error handler is placed at the bottom. But the Try/Catch construct is so elegent. It's a very structured way of handling errors and is very easy to follow. This pattern attempts to reproduce that in a very clean concise way. The flow is very consistent and doesn't jump from place to place.

Sub InLineErrorHandling()
 'code without error handling
BeginTry1:
 'activate inline error handler
 On Error GoTo ErrHandler1
 'code block that may result in an error
 Dim a As String: a = "Abc"
 Dim c As Integer: c = a 'type mismatch
ErrHandler1:
 'handle the error
 If Err.Number <> 0 Then
 'the error handler is now active
 Debug.Print (Err.Description)
 End If
 'disable previous error handler (VERY IMPORTANT)
 On Error GoTo 0
 'exit the error handler
 Resume EndTry1
EndTry1:
 'more code with or without error handling
End Sub

Sources:

Properly managed this works quite nicely. It is a very clean flowing pattern that is reproducible anywhere it is needed.

deleted 10 characters in body
Source Link
D_Bester
  • 201
  • 2
  • 7

This answer is intended to simplify the Try/Catch pattern to be easily understandable.

This is not very different from regular inline error handling except that it can skip multiple lines at once, handle an error and then resume regular execution. This is a very cleanly structured pattern for handling an error. The flow moves very cleanly from top to bottom; no spaghetti code here.

Traditionally the error handler is placed at the bottom. But the Try/Catch construct is so elegent. It's a very structured way of handling errors and is very easy to follow. This pattern attempts to reproduce that in a very clean concise way. The flow is very consistent and doesn't jump from place to place.

Sub InLineErrorHandling()
 'code without error handling
BeginTry1:
 'activate inline error handler
 On Error GoTo ErrHandler1
 'code block that may result in an error
 Dim a As String: a = "Abc"
 Dim c As Integer: c = a 'type mismatch
ErrHandler1:
 'handle the error
 If Err.Number <> 0 Then
 'the error handler is now active
 Debug.Print (Err.Description)
 End If
 'disable previous error handler (VERY IMPORTANT)
 On Error GoTo 0
 'clear the error object AND exit'exit the error handler
 Resume EndTry1
EndTry1:
 'more code with or without error handling
End Sub

Sources:

Properly managed this works quite nicely. It is a very clean flowing pattern that is reproducible anywhere it is needed.

This answer is intended to simplify the Try/Catch pattern to be easily understandable.

This is not very different from regular inline error handling except that it can skip multiple lines at once, handle an error and then resume regular execution. This is a very cleanly structured pattern for handling an error. The flow moves very cleanly from top to bottom; no spaghetti code here.

Traditionally the error handler is placed at the bottom. But the Try/Catch construct is so elegent. It's a very structured way of handling errors and is very easy to follow. This pattern attempts to reproduce that in a very clean concise way. The flow is very consistent and doesn't jump from place to place.

Sub InLineErrorHandling()
 'code without error handling
BeginTry1:
 'activate inline error handler
 On Error GoTo ErrHandler1
 'code block that may result in an error
 Dim a As String: a = "Abc"
 Dim c As Integer: c = a 'type mismatch
ErrHandler1:
 'handle the error
 If Err.Number <> 0 Then
 'the error handler is now active
 Debug.Print (Err.Description)
 End If
 'disable previous error handler
 On Error GoTo 0
 'clear the error object AND exit the error handler
 Resume EndTry1
EndTry1:
 'more code with or without error handling
End Sub

Sources:

Properly managed this works quite nicely. It is a very clean flowing pattern that is reproducible anywhere it is needed.

This answer is intended to simplify the Try/Catch pattern to be easily understandable.

This is not very different from regular inline error handling except that it can skip multiple lines at once, handle an error and then resume regular execution. This is a very cleanly structured pattern for handling an error. The flow moves very cleanly from top to bottom; no spaghetti code here.

Traditionally the error handler is placed at the bottom. But the Try/Catch construct is so elegent. It's a very structured way of handling errors and is very easy to follow. This pattern attempts to reproduce that in a very clean concise way. The flow is very consistent and doesn't jump from place to place.

Sub InLineErrorHandling()
 'code without error handling
BeginTry1:
 'activate inline error handler
 On Error GoTo ErrHandler1
 'code block that may result in an error
 Dim a As String: a = "Abc"
 Dim c As Integer: c = a 'type mismatch
ErrHandler1:
 'handle the error
 If Err.Number <> 0 Then
 'the error handler is now active
 Debug.Print (Err.Description)
 End If
 'disable previous error handler (VERY IMPORTANT)
 On Error GoTo 0
 'exit the error handler
 Resume EndTry1
EndTry1:
 'more code with or without error handling
End Sub

Sources:

Properly managed this works quite nicely. It is a very clean flowing pattern that is reproducible anywhere it is needed.

deleted 143 characters in body
Source Link
D_Bester
  • 201
  • 2
  • 7

I've done this before. What I've learned from your exampleThis answer is On Error Goto -1. I've updated my code with thatintended to simplify the Try/Catch pattern to be easily understandable.

This is not very different from regular inline error handling except that it can skip multiple lines at once, handle an error and then resume regular execution. This is a very cleanly structured pattern for handling an error. The flow moves very cleanly from top to bottom; no spaghetti code here.

Traditionally the error handler is placed at the bottom. But the Try/Catch construct is so elegent. It's a very structured way of handling errors and is very easy to follow. This pattern attempts to reproduce that in a very clean concise way. The flow is very consistent and doesn't jump from place to place.

Sub InLineErrorHandling()
 'code without error handling
BeginTry1:
 'activate inline error handler
 On Error GoTo ErrHandler1
 'code block that may result in an error
 Dim a As String: a = "Abc"
 Dim c As Integer: c = a 'type mismatch
ErrHandler1:
 'handle the error
 If Err.Number <> 0 Then
 'the error handler is now active
 MsgBoxDebug.Print (Err.Description)
 End If
 'disable previous Enderror Ifhandler
 On Error GoTo 0
 'clear the error object AND exit the error handler
 On Error GoToResume -1EndTry1
EndTry1:
 'more code with or without error handling

End Sub

Sources:

Properly managed this works quite nicely. It is a very clean flowing pattern that is reproducible anywhere it is needed.

The BeginTry1: and EndTry1: labels are of course superfluous but they help define the code pattern.

I've done this before. What I've learned from your example is On Error Goto -1. I've updated my code with that.

This is not very different from regular inline error handling except that it can skip multiple lines at once, handle an error and then resume regular execution. This is a very cleanly structured pattern for handling an error. The flow moves very cleanly from top to bottom; no spaghetti code here.

Traditionally the error handler is placed at the bottom. But the Try/Catch construct is so elegent. It's a very structured way of handling errors and is very easy to follow. This pattern attempts to reproduce that in a very clean concise way. The flow is very consistent and doesn't jump from place to place.

Sub InLineErrorHandling()
 'code without error handling
BeginTry1:
 'activate inline error handler
 On Error GoTo ErrHandler1
 'code block that may result in an error
 Dim a As String: a = "Abc"
 Dim c As Integer: c = a 'type mismatch
ErrHandler1:
 'handle the error
 If Err.Number <> 0 Then
 'the error handler is now active
 MsgBox (Err.Description)
 End If
 'clear the error object AND exit the error handler
 On Error GoTo -1
EndTry1:
 'more code with or without error handling

End Sub

Sources:

Properly managed this works quite nicely. It is a very clean flowing pattern that is reproducible anywhere it is needed.

The BeginTry1: and EndTry1: labels are of course superfluous but they help define the code pattern.

This answer is intended to simplify the Try/Catch pattern to be easily understandable.

This is not very different from regular inline error handling except that it can skip multiple lines at once, handle an error and then resume regular execution. This is a very cleanly structured pattern for handling an error. The flow moves very cleanly from top to bottom; no spaghetti code here.

Traditionally the error handler is placed at the bottom. But the Try/Catch construct is so elegent. It's a very structured way of handling errors and is very easy to follow. This pattern attempts to reproduce that in a very clean concise way. The flow is very consistent and doesn't jump from place to place.

Sub InLineErrorHandling()
 'code without error handling
BeginTry1:
 'activate inline error handler
 On Error GoTo ErrHandler1
 'code block that may result in an error
 Dim a As String: a = "Abc"
 Dim c As Integer: c = a 'type mismatch
ErrHandler1:
 'handle the error
 If Err.Number <> 0 Then
 'the error handler is now active
 Debug.Print (Err.Description)
 End If
 'disable previous error handler
 On Error GoTo 0
 'clear the error object AND exit the error handler
 Resume EndTry1
EndTry1:
 'more code with or without error handling
End Sub

Sources:

Properly managed this works quite nicely. It is a very clean flowing pattern that is reproducible anywhere it is needed.

Post Migrated Here from stackoverflow.com (revisions)
Source Link
D_Bester
  • 201
  • 2
  • 7
Loading
lang-vb

AltStyle によって変換されたページ (->オリジナル) /