You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+17-16Lines changed: 17 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -160,42 +160,43 @@ A range adaptor that takes a range of `Result<T, E>` and returns a view containi
160
160
161
161
## Exception-Based Error Handling
162
162
163
-
When exceptions are enabled (i.e. `_CPPUNWIND`, `__EXCEPTIONS`, or `__cpp_exceptions` are defined), cppmatch also provides exception-based alternatives to handle error propagation and pattern matching.
163
+
When exceptions are enabled cppmatch also provides exception-based alternatives to handle error propagation and pattern matching, which supports MSVC
164
164
165
-
### `expect_e(expr)`
166
-
The function `expect_e` is an alternative to the `expect` macro. It evaluates an expression that returns a `Result<T, E>`. If the result is an error, it uses pattern matching to throw the contained error as an exception. Otherwise, it returns the success value.
165
+
### `expect_e(expr)`
166
+
The **function**`expect_e` is an alternative to the **macro**`expect`. It evaluates an expression that returns a `Result<T, E>`. If the result is an error, it uses pattern matching to throw the contained error as an exception. Otherwise, it returns the success value.
167
+
168
+
The try...catch will be generated dynamically with the macro **match_e**, surrounding the expression between try/catch is not necessary
The `match_e` macro allows you to perform pattern matching on the result of a function call that returns a `Result`. Unlike `match`, it supports exceptions: if the function call throws an exception (via `expect_e` or otherwise), `match_e` catches it and applies the provided lambdas to handle the exception.
185
182
183
+
match_e will **AUTOMATICALLY** surround the expression EXPR in a try...catch block that handles all the Error types returned by EXPR.
184
+
185
+
If an error is thrown that was not specified in match_e, it will be rethrown.
186
+
If only using cpp-match, this is minimized because in match_e you have to exhaustively pass a callable to all possible variant returns of parse_int.
187
+
186
188
- **Usage Details:**
187
189
- **EXPR:** Must be a function call expression (not an lvalue), as enforced by a static assertion.
188
190
- **Lambdas:** A set of lambdas to match the success or error cases.
189
191
- **Note:** This macro is only available when exceptions are enabled.
190
192
191
193
- **Example:**
192
194
```cpp
193
-
auto result_message = cppmatch::match_e(parse_int("abc"),
// If parse_int("abc") returns an error, the error lambda is executed.
198
-
// Otherwise, the success lambda is executed.
199
+
199
200
```
200
201
201
202
Under the hood, `match_e` wraps the function call in a lambda, executes it, and if an exception is thrown, it attempts to match the exception against the provided lambdas by iterating through the possible error types. If no match is found, it rethrows the exception.
0 commit comments