try

Similar to catch, but returns an Either result which is (Right a) if no exception of type e was raised, or (Left ex) if an exception of type e was raised and its value is ex. If any other type of exception is raised then it will be propagated up to the next enclosing exception handler.
try a = catch (Right `liftM` a) (return . Left)
The parser try p behaves like parser p, except that it pretends that it hasn't consumed any input when an error occurs. This combinator is used whenever arbitrary look ahead is needed. Since it pretends that it hasn't consumed any input when p fails, the (<|>) combinator will try its second alternative even when the first parser failed while consuming input. The try combinator can for example be used to distinguish identifiers and reserved words. Both reserved words and identifiers are a sequence of letters. Whenever we expect a certain reserved word where we can also expect an identifier we have to use the try combinator. Suppose we write:
expr        = letExpr <|> identifier <?> "expression"

letExpr     = do{ string "let"; ... }
identifier  = many1 letter
If the user writes "lexical", the parser fails with: unexpected 'x', expecting 't' in "let". Indeed, since the (<|>) combinator only tries alternatives when the first alternative hasn't consumed input, the identifier parser is never tried (because the prefix "le" of the string "let" parser is already consumed). The right behaviour can be obtained by adding the try combinator:
expr        = letExpr <|> identifier <?> "expression"

letExpr     = do{ try (string "let"); ... }
identifier  = many1 letter
Similar to catch, but returns an Either result. See Control.Exception's try.
Attempt a parse, and if it fails, rewind the input so that no input appears to have been consumed. This combinator is provided for compatibility with Parsec. attoparsec parsers always backtrack on failure.
The parser try p behaves like the parser p, except that it backtracks the parser state when p fails (either consuming input or not). This combinator is used whenever arbitrary look ahead is needed. Since it pretends that it hasn't consumed any input when p fails, the (<|>) combinator will try its second alternative even if the first parser failed while consuming input. For example, here is a parser that is supposed to parse the word “let” or the word “lexical”:
>>> parseTest (string "let" <|> string "lexical") "lexical"
1:1:
unexpected "lex"
expecting "let"
What happens here? The first parser consumes “le” and fails (because it doesn't see a “t”). The second parser, however, isn't tried, since the first parser has already consumed some input! try fixes this behavior and allows backtracking to work:
>>> parseTest (try (string "let") <|> string "lexical") "lexical"
"lexical"
try also improves error messages in case of overlapping alternatives, because Megaparsec's hint system can be used:
>>> parseTest (try (string "let") <|> string "lexical") "le"
1:1:
unexpected "le"
expecting "let" or "lexical"
Note that as of Megaparsec 4.4.0, string backtracks automatically (see tokens), so it does not need try. However, the examples above demonstrate the idea behind try so well that it was decided to keep them. You still need to use try when your alternatives are complex, composite parsers.
Run the given action and catch any synchronous exceptions as a Left value. This is parameterized on the exception type. To catch all synchronous exceptions, use tryAny.
Same as upstream try, but will not catch asynchronous exceptions
Generalized version of try. Note, when the given computation throws an exception any monadic side effects in m will be discarded.
Support for API calls that are passed a fixed-size buffer and tell you via the return value if the buffer was too small. In that case, we extend the buffer size and try again.
Support for API calls that are passed a fixed-size buffer and tell you via the return value if the buffer was too small. In that case, we extend the buffer size and try again.
The parser try p behaves like parser p, except that it pretends that it hasn't consumed any input when an error occurs. This combinator is used whenever arbitrary look ahead is needed. Since it pretends that it hasn't consumed any input when p fails, the (<|>) combinator will try its second alternative even when the first parser failed while consuming input. The try combinator can for example be used to distinguish identifiers and reserved words. Both reserved words and identifiers are a sequence of letters. Whenever we expect a certain reserved word where we can also expect an identifier we have to use the try combinator. Suppose we write:
expr        = letExpr <|> identifier <?> "expression"

letExpr     = do{ string "let"; ... }
identifier  = many1 letter
If the user writes "lexical", the parser fails with: unexpected 'x', expecting 't' in "let". Indeed, since the (<|>) combinator only tries alternatives when the first alternative hasn't consumed input, the identifier parser is never tried (because the prefix "le" of the string "let" parser is already consumed). The right behaviour can be obtained by adding the try combinator:
expr        = letExpr <|> identifier <?> "expression"

letExpr     = do{ try (string "let"); ... }
identifier  = many1 letter
Take a parser that may consume input, and on failure, go back to where we started and fail as if we didn't consume input.
Similar to catch, but returns an Either result which is (Right a) if no exception of type e was thrown, or (Left ex) if an exception of type e was thrown and its value is ex.
Deprecated: Use Control.Monad.Catch.try instead
Lifted try.
Same as upstream try, but will not catch asynchronous exceptions
Return either the result of a Lua computation or, if an exception was thrown, the error.
Like try, but can also handle an AnnotatedException or the underlying value. Useful when you want to try to catch a type of exception, but you may not care about the Annotations that it may or may not have. Example:
Left exn <- try $ throw (AnnotatedException [] TestException)
exn == TestException
Left exn <- try $ throw TestException
exn == AnnotatedException [] TestException
Like try but uses MonadUnliftIO instead of MonadCatch.
Returns Left e if the computation throws an exception e, or Right a if it returns a result a.