Skip to content

Cimplex/CSharp-Try

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

CSharp-Try

A better way to Try/Catch in C#

Examples

Use as an Easy Convert.To Proxy:

C# (Version Pre-7.0)

string input = "false"
bool? output = null;
Try.Run(Convert.ToBoolean, input, out output);

if (output.HasValue) {
  // Do stuff with "output.Value"
}

C# (Version 7.0)

string input = "false"
Try.Run(Convert.ToBoolean, input, out bool? output);

if (output.HasValue) {
  // Do stuff with "output.Value"
}

Use as a Cleaner Try, Multiple Catch Statement:

var request = System.Net.HttpWebRequest.Create("https://www.google.com");

System.Net.WebResponse response;
Exception exception = Try.Run(request.GetResponse, out response);

if (exception is NotSupportedException)
    Console.WriteLine("We got a 'NotSupportedException' exception :(");

else if (exception is Exception)
    Console.WriteLine("We got an exception, but don't care what it is...");

else
{
    // Use 'response' to get our http information
}

About

A better way to Try/Catch in C#

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages