{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/csharplang/issues/220","repository_url":"https://api.github.com/repos/dotnet/csharplang","labels_url":"https://api.github.com/repos/dotnet/csharplang/issues/220/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/csharplang/issues/220/comments","events_url":"https://api.github.com/repos/dotnet/csharplang/issues/220/events","html_url":"https://github.com/dotnet/csharplang/issues/220","id":211615697,"number":220,"title":"Proposal: `try` expression without `catch` for inline use","user":{"login":"lachbaer","id":17041498,"avatar_url":"https://avatars3.githubusercontent.com/u/17041498?v=3","gravatar_id":"","url":"https://api.github.com/users/lachbaer","html_url":"https://github.com/lachbaer","followers_url":"https://api.github.com/users/lachbaer/followers","following_url":"https://api.github.com/users/lachbaer/following{/other_user}","gists_url":"https://api.github.com/users/lachbaer/gists{/gist_id}","starred_url":"https://api.github.com/users/lachbaer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lachbaer/subscriptions","organizations_url":"https://api.github.com/users/lachbaer/orgs","repos_url":"https://api.github.com/users/lachbaer/repos","events_url":"https://api.github.com/users/lachbaer/events{/privacy}","received_events_url":"https://api.github.com/users/lachbaer/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2017-03-03T07:34:10Z","updated_at":"2017-03-03T07:53:21Z","closed_at":null,"body":"## Proposal: `try` expression without `catch` for inline use\r\n### Intent\r\nSometimes it is not necessary to catch an exception, because you can either check side conditions or proceed though the expression is failed. In these cases it would be nice to skip the exception checking.\r\n### Example\r\nIn current code there would be something like\r\n```C#\r\ntry {\r\n    var textStream = new StreamReader(\"C:\\nonexistingfile.txt\");\r\n    ProcessFile(textStream);\r\n}\r\ncatch { }\r\nGoOnWithOtherThings();\r\n```\r\nor even wrap it additionally with\r\n```C#\r\nvar fileName = @\"C:\\notexisting.txt\";\r\nif (File.Exists(fileName)) {\r\n   // try block from above\r\n}\r\nGoOnWithOtherThings();\r\n```\r\nThis could be abbrevated and streamlined drastically with\r\n```C#\r\nvar textStream = try new StreamReader(\"C:\\nonexistingfile.txt\");\r\nif (textStream != null) ProcessFile(textStream);\r\nGoOnWithOtherThings();\r\n```\r\nThe catching isn't necessary here, because a null-check (that should be done in `ProcessFile()` anyway) already cares about the failure. A complete `try { } catch { }` is just unnecessary boilerplate.\r\n\r\nI guess that there are plenty of other useful scenarios where this would come in handy.\r\n\r\n### Other languages\r\nPHP uses the `@` operator before an expression to suppress errors and warnings\r\n```PHP\r\n$fp = @fsockopen(\"www.example.com\", 80, $errno, $errstr, 30);\r\nif (!$fp) {\r\n    echo \"$errstr ($errno)<br />\\n\";\r\n```\r\nIn PHP `@` is used quite often to shorten things.\r\n\r\nNow, open for discussion... 😄 "},"comment":{"url":"https://api.github.com/repos/dotnet/csharplang/issues/comments/283889295","html_url":"https://github.com/dotnet/csharplang/issues/220#issuecomment-283889295","issue_url":"https://api.github.com/repos/dotnet/csharplang/issues/220","id":283889295,"user":{"login":"lachbaer","id":17041498,"avatar_url":"https://avatars3.githubusercontent.com/u/17041498?v=3","gravatar_id":"","url":"https://api.github.com/users/lachbaer","html_url":"https://github.com/lachbaer","followers_url":"https://api.github.com/users/lachbaer/followers","following_url":"https://api.github.com/users/lachbaer/following{/other_user}","gists_url":"https://api.github.com/users/lachbaer/gists{/gist_id}","starred_url":"https://api.github.com/users/lachbaer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lachbaer/subscriptions","organizations_url":"https://api.github.com/users/lachbaer/orgs","repos_url":"https://api.github.com/users/lachbaer/repos","events_url":"https://api.github.com/users/lachbaer/events{/privacy}","received_events_url":"https://api.github.com/users/lachbaer/received_events","type":"User","site_admin":false},"created_at":"2017-03-03T07:53:21Z","updated_at":"2017-03-03T07:53:21Z","body":"And by the way, actually it wouldn't be much different than using `TryParse()` - a framework supported feature.\r\nOther question: haven't you too already seen used code with empty `catch` blocks anywhere? If not, your bad 😉 "}}
{"actor":{"display_login":"lachbaer"}}