{"id":46,"date":"2009-06-14T14:03:27","date_gmt":"2009-06-14T13:03:27","guid":{"rendered":"http:\/\/benjiweber.co.uk\/blog\/?p=46"},"modified":"2009-06-14T14:26:02","modified_gmt":"2009-06-14T13:26:02","slug":"java-abuse-ternary-trycatch","status":"publish","type":"post","link":"https:\/\/benjiweber.co.uk\/blog\/2009\/06\/14\/java-abuse-ternary-trycatch\/","title":{"rendered":"Java Abuse &#8211; Ternary Try\/Catch"},"content":{"rendered":"<p class=\"lead\">We often discuss Java limitations on IRC and try to come up with (sometimes silly) workarounds. Unfortunately after time passes it&#8217;s often easy to forget the outcome, and lose code snippets. So I thought I&#8217;d start blogging some of them so I don&#8217;t lose them, and other people might suggest other ways of doing things that we&#8217;ve overlooked.<\/p>\n<p>This particular problem occurs when you want to assign the result of a method that can throw an exception to a final variable. For example:<\/p>\n<pre lang=\"Java\"> \r\nfinal Customer c;\r\ntry\r\n{\r\n\tc = getCustomer(id);\r\n} catch (CustomerNotFoundException e)\r\n{\r\n\tc = createNewCustomer();\r\n}\r\n<\/pre>\n<p>This will fail to compile with &#8220;variable c might already have been assigned&#8221;. Of course making c not final would solve the problem, but that&#8217;s no fun.<\/p>\n<p>If we were not using Exceptions, Java provides a useful ternary operator &#8220;?:&#8221; that lets us do things like:<\/p>\n<pre lang=\"Java\"> \r\nfinal Customer c = customerExists(id) ? getCustomer(id) : createNewCustomer();\r\n<\/pre>\n<p>Which is nice and clean, but means that getCustomer is going to have to return null or throw a RuntimeException in the case that there is no matching customer, which is undesirable. Also  customerExists() may be expensive.<\/p>\n<p>We could also possibly use something along the lines of<\/p>\n<pre lang=\"Java\"> \r\nfinal Option<Customer> c = getCustomer();\r\n<\/pre>\n<p>Both of these alternatives, however, require changing the API you&#8217;re consuming, and avoiding Exceptions. It would be nice if there was an equivalent of &#8220;?:&#8221; for try\/catch so that you could assign the result to a final variable. The best I can manage in Java is below, can anyone do better?<\/p>\n<pre lang=\"Java\"> \r\nimport java.lang.reflect.ParameterizedType;\r\n\r\nabstract class TryCatch<T, U extends Exception>\r\n{\r\n\tpublic T value()\r\n\t{\r\n\t\ttry\r\n\t\t{\r\n\t\t\treturn Try();\r\n\t\t} catch (Exception e)\r\n\t\t{\r\n\t\t\tif (getTypeOfU().isAssignableFrom(e.getClass()))\r\n\t\t\t{\r\n\t\t\t\treturn Catch();\r\n\t\t\t} else\r\n\t\t\t{\r\n\t\t\t\tthrow new RuntimeException(e);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\t@SuppressWarnings(\"unchecked\")\r\n\tprivate Class<U> getTypeOfU()\r\n\t{\r\n\t\treturn (Class<U>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[1];\r\n\t}\r\n\r\n\tpublic abstract T Try() throws U;\r\n\r\n\tpublic abstract T Catch();\r\n}\r\n\r\n\/\/Example\r\npublic class Main\r\n{\r\n\tprivate static CustomerRepo repo = new CustomerRepo();\r\n\r\n\tpublic static void main(String[] args) \r\n\t{\r\n\t\tfinal Customer c = new TryCatch<Customer, CustomerNotFoundException>()\r\n\t\t{\r\n\t\t\tpublic Customer Try() throws CustomerNotFoundException\r\n\t\t\t{\r\n\t\t\t\tSystem.out.println(\"in try\");\r\n\t\t\t\treturn repo.getCustomer(1);\r\n\t\t\t}\r\n\r\n\t\t\tpublic Customer Catch()\r\n\t\t\t{\r\n\t\t\t\tSystem.out.println(\"in catch\");\r\n\t\t\t\treturn repo.createCustomer();\r\n\t\t\t}\r\n\t\t}.value();\r\n\t}\r\n\r\n}\r\n\r\nclass CustomerRepo\r\n{\r\n\r\n\tpublic Customer getCustomer(int id) throws CustomerNotFoundException\r\n\t{\r\n\t\tthrow new CustomerNotFoundException();\r\n\t}\r\n\r\n\tpublic Customer createCustomer()\r\n\t{\r\n\t\treturn new Customer();\r\n\t}\r\n}\r\n\r\nclass Customer\r\n{\r\n}\r\n\r\nclass CustomerNotFoundException extends Exception\r\n{\r\n}\r\n<\/pre>\n<p>In C# we don&#8217;t run into the same problem since <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/acdd6hb7.aspx\">readonly<\/a> is much less useful than Java&#8217;s <a href=\"http:\/\/mindprod.com\/jgloss\/final.html\">final<\/a>. However, if we wanted to try\/catch at the same time we can do a bit better. Here&#8217;s an alternative in C#:<\/p>\n<pre lang=\"CSharp\"> \r\nusing System;\r\n\r\npublic class Example\r\n{\r\n\tpublic static void Main()\r\n\t{\r\n\t\tExample t = new Example();\r\n\t\tt.Foo();\r\n\t}\r\n\r\n\tpublic void Foo()\r\n\t{\r\n\t\tString result1 = this.Try(() => GetBar(true)).Catch<BarException>(() => \"Caught a BarException\");\r\n\t\tString result2 = this.Try(() => GetBar(false)).Catch<BarException>(() => \"Caught a BarException\");\r\n\t\tConsole.WriteLine(result1);\r\n\t\tConsole.WriteLine(result2);\r\n\t}\r\n\r\n\tpublic String GetBar(bool succeed)\r\n\t{\r\n\t\tif (succeed)\r\n\t\t\treturn \"Success!\";\r\n\t\telse\r\n\t\t\tthrow new BarException();\r\n\t}\r\n}\r\n\r\npublic class BarException : Exception {}\r\n\r\npublic class Tryer<TResult>\r\n{\r\n\tprivate readonly Func<TResult> toTry;\r\n\tinternal Tryer(Func<TResult> toTry)\r\n\t{\r\n\t\tthis.toTry = toTry;\r\n\t}\r\n\r\n\tpublic TResult Catch<TException>(Func<TResult> whenCaught)\r\n\t\twhere TException : Exception\r\n\t{\r\n\t\ttry\r\n\t\t{\r\n\t\t\treturn toTry();\r\n\t\t} catch (TException)\r\n\t\t{\r\n\t\t\treturn whenCaught();\r\n\t\t}\r\n\t}\r\n}\r\n\r\nnamespace System\r\n{\r\n\tpublic static class ProvidesTry\r\n\t{\r\n\t\tpublic static Tryer<TResult> Try<T,TResult>(this T other, Func<TResult> toTry)\r\n\t\t{\r\n\t\t\treturn new Tryer<TResult>(toTry);\r\n\t\t} \r\n\t}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>We often discuss Java limitations on IRC and try to come up with (sometimes silly) workarounds. Unfortunately after time passes it&#8217;s often easy to forget the outcome, and lose code snippets. So I thought I&#8217;d start blogging some of them so I don&#8217;t lose them, and other people might suggest other ways of doing things&#8230;  <a href=\"https:\/\/benjiweber.co.uk\/blog\/2009\/06\/14\/java-abuse-ternary-trycatch\/\" class=\"more-link\" title=\"Read Java Abuse &#8211; Ternary Try\/Catch\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,1],"tags":[23],"class_list":["post-46","post","type-post","status-publish","format-standard","hentry","category-java","category-uncategorized","tag-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java Abuse - Ternary Try\/Catch - Benji&#039;s Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/benjiweber.co.uk\/blog\/2009\/06\/14\/java-abuse-ternary-trycatch\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Abuse - Ternary Try\/Catch - Benji&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"We often discuss Java limitations on IRC and try to come up with (sometimes silly) workarounds. Unfortunately after time passes it&#8217;s often easy to forget the outcome, and lose code snippets. So I thought I&#8217;d start blogging some of them so I don&#8217;t lose them, and other people might suggest other ways of doing things... Read more &raquo;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/benjiweber.co.uk\/blog\/2009\/06\/14\/java-abuse-ternary-trycatch\/\" \/>\n<meta property=\"og:site_name\" content=\"Benji&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2009-06-14T13:03:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2009-06-14T13:26:02+00:00\" \/>\n<meta name=\"author\" content=\"benji\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"benji\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2009\\\/06\\\/14\\\/java-abuse-ternary-trycatch\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2009\\\/06\\\/14\\\/java-abuse-ternary-trycatch\\\/\"},\"author\":{\"name\":\"benji\",\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/#\\\/schema\\\/person\\\/45ecb36b51f4ce99e6929d2d31ca5c09\"},\"headline\":\"Java Abuse &#8211; Ternary Try\\\/Catch\",\"datePublished\":\"2009-06-14T13:03:27+00:00\",\"dateModified\":\"2009-06-14T13:26:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2009\\\/06\\\/14\\\/java-abuse-ternary-trycatch\\\/\"},\"wordCount\":281,\"commentCount\":0,\"keywords\":[\"Java\"],\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2009\\\/06\\\/14\\\/java-abuse-ternary-trycatch\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2009\\\/06\\\/14\\\/java-abuse-ternary-trycatch\\\/\",\"url\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2009\\\/06\\\/14\\\/java-abuse-ternary-trycatch\\\/\",\"name\":\"Java Abuse - Ternary Try\\\/Catch - Benji&#039;s Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/#website\"},\"datePublished\":\"2009-06-14T13:03:27+00:00\",\"dateModified\":\"2009-06-14T13:26:02+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/#\\\/schema\\\/person\\\/45ecb36b51f4ce99e6929d2d31ca5c09\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2009\\\/06\\\/14\\\/java-abuse-ternary-trycatch\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2009\\\/06\\\/14\\\/java-abuse-ternary-trycatch\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2009\\\/06\\\/14\\\/java-abuse-ternary-trycatch\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Abuse &#8211; Ternary Try\\\/Catch\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/\",\"name\":\"Benji&#039;s Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/#\\\/schema\\\/person\\\/45ecb36b51f4ce99e6929d2d31ca5c09\",\"name\":\"benji\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/417ada828fa4be09324820d24c0d93c3bfcbae183629e09117c9f4c4ab7ed43a?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/417ada828fa4be09324820d24c0d93c3bfcbae183629e09117c9f4c4ab7ed43a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/417ada828fa4be09324820d24c0d93c3bfcbae183629e09117c9f4c4ab7ed43a?s=96&d=mm&r=g\",\"caption\":\"benji\"},\"sameAs\":[\"http:\\\/\\\/benjiweber.co.uk\"],\"url\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/author\\\/benji\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Abuse - Ternary Try\/Catch - Benji&#039;s Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/benjiweber.co.uk\/blog\/2009\/06\/14\/java-abuse-ternary-trycatch\/","og_locale":"en_US","og_type":"article","og_title":"Java Abuse - Ternary Try\/Catch - Benji&#039;s Blog","og_description":"We often discuss Java limitations on IRC and try to come up with (sometimes silly) workarounds. Unfortunately after time passes it&#8217;s often easy to forget the outcome, and lose code snippets. So I thought I&#8217;d start blogging some of them so I don&#8217;t lose them, and other people might suggest other ways of doing things... Read more &raquo;","og_url":"https:\/\/benjiweber.co.uk\/blog\/2009\/06\/14\/java-abuse-ternary-trycatch\/","og_site_name":"Benji&#039;s Blog","article_published_time":"2009-06-14T13:03:27+00:00","article_modified_time":"2009-06-14T13:26:02+00:00","author":"benji","twitter_card":"summary_large_image","twitter_misc":{"Written by":"benji","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/benjiweber.co.uk\/blog\/2009\/06\/14\/java-abuse-ternary-trycatch\/#article","isPartOf":{"@id":"https:\/\/benjiweber.co.uk\/blog\/2009\/06\/14\/java-abuse-ternary-trycatch\/"},"author":{"name":"benji","@id":"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09"},"headline":"Java Abuse &#8211; Ternary Try\/Catch","datePublished":"2009-06-14T13:03:27+00:00","dateModified":"2009-06-14T13:26:02+00:00","mainEntityOfPage":{"@id":"https:\/\/benjiweber.co.uk\/blog\/2009\/06\/14\/java-abuse-ternary-trycatch\/"},"wordCount":281,"commentCount":0,"keywords":["Java"],"articleSection":["Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/benjiweber.co.uk\/blog\/2009\/06\/14\/java-abuse-ternary-trycatch\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/benjiweber.co.uk\/blog\/2009\/06\/14\/java-abuse-ternary-trycatch\/","url":"https:\/\/benjiweber.co.uk\/blog\/2009\/06\/14\/java-abuse-ternary-trycatch\/","name":"Java Abuse - Ternary Try\/Catch - Benji&#039;s Blog","isPartOf":{"@id":"https:\/\/benjiweber.co.uk\/blog\/#website"},"datePublished":"2009-06-14T13:03:27+00:00","dateModified":"2009-06-14T13:26:02+00:00","author":{"@id":"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09"},"breadcrumb":{"@id":"https:\/\/benjiweber.co.uk\/blog\/2009\/06\/14\/java-abuse-ternary-trycatch\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/benjiweber.co.uk\/blog\/2009\/06\/14\/java-abuse-ternary-trycatch\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/benjiweber.co.uk\/blog\/2009\/06\/14\/java-abuse-ternary-trycatch\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/benjiweber.co.uk\/blog\/"},{"@type":"ListItem","position":2,"name":"Java Abuse &#8211; Ternary Try\/Catch"}]},{"@type":"WebSite","@id":"https:\/\/benjiweber.co.uk\/blog\/#website","url":"https:\/\/benjiweber.co.uk\/blog\/","name":"Benji&#039;s Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/benjiweber.co.uk\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09","name":"benji","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/417ada828fa4be09324820d24c0d93c3bfcbae183629e09117c9f4c4ab7ed43a?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/417ada828fa4be09324820d24c0d93c3bfcbae183629e09117c9f4c4ab7ed43a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/417ada828fa4be09324820d24c0d93c3bfcbae183629e09117c9f4c4ab7ed43a?s=96&d=mm&r=g","caption":"benji"},"sameAs":["http:\/\/benjiweber.co.uk"],"url":"https:\/\/benjiweber.co.uk\/blog\/author\/benji\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/46","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=46"}],"version-history":[{"count":25,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/46\/revisions"}],"predecessor-version":[{"id":71,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/46\/revisions\/71"}],"wp:attachment":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=46"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=46"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=46"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}