question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Documentation on how to use Try<T>

See original GitHub issue

I’m not sure how to use it, how to create a Try, what implicitly converts to them and how exactly they work.

e.g. will this create a Try or break out of it:

                catch (Exception e)
                {
                    if (e is MailAddressException || e is MailServiceException)
                    {
                         throw e;
                    }
                }

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
louthycommented, Oct 4, 2015

@fazouane-marouane

irrelevant to the issue: @louthy Thanks for your awesome work! I’ve just discovered LanguageExt an hour ago. This project implements all the most needed/helpful paradigms seen in other systems & languages. Thanks for bringing in this into C#

Thank you, that’s very nice to hear! 😃

1reaction
louthycommented, Oct 2, 2015

Try<T> is a delegate, so you should use it like so:

In C# 6

    Try<int> Divide(int x, int y) => () => 
        x / y;

In C# 5 or older

    Try<int> Divide(int x, int y)
    { 
        return () => x / y;
    }

Calling Divide with zero for y will return a Try<int> which when matched upon will give a Fail state, otherwise a Succ state, i.e.:

    var res = match( Divide(10,0),
                     Succ: v => v,
                     Fail: 0 );

The primary benefit is that the exception is treated as a return value like anything else, and you can chain the Trys together in a LINQ statement where an exception will cause the expression to ‘early out’

Take a look at the unit tests for more examples: https://github.com/louthy/language-ext/blob/master/LanguageExt.Tests/TryMonadTests.cs

TryOption<T> gives the 3 possible states a function can return:

  • Some value
  • No value (usually null is used for this)
  • Exceptional value

https://github.com/louthy/language-ext/blob/master/LanguageExt.Tests/TryOptionMonadTests.cs

Read more comments on GitHub >

github_iconTop Results From Across the Web

TRY / EXCEPT / FINALLY exception catching and handling ...
How to use TRY / EXCEPT / FINALLY to catch and handle errors in Robot Framework. ... Documentation Robot Framework 5 syntax examples....
Read more >
Python Try Except
The try block lets you test a block of code for errors. The except block lets you handle the ... These exceptions can...
Read more >
Error Handling | Documentation - Swift.org
method propagates any errors it throws, any code that calls this method must either handle the errors — using a do - catch...
Read more >
Python Tutorial: Using Try/Except Blocks for Error Handling
We've all run into errors and exceptions while writing Python programs. In this video, we will learn how we can handle exceptions in ......
Read more >
Try
Usage. Try. // Your code. Catch [ErrorParameter] [As ErrorType] ... If used, the Try statement will handle only that type of runtime error....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found