Since the release of version 8, Java has been taking great strides to facilitate writing expressive and functional code. Unfortunately, these strides are often tripped up by one of Java's oldest and most pervasive features: exceptions. This post will suggest how to handle exceptions seamlessly in functional Java code, but first, we must examine the problems raised when an exception is encountered in one of Java's core functional APIs. Errors In Functional Java APIs So, what happens when an exception-throwing method is called in a lambda? Let's find out by writing a method that parses a URI from an environment variable. Since System.getenv can return null, we'll use Optional to convert the nullable return value into a URI: /** * Get the API's base URL from the environment. * @return The URI, or null if the environment variable is missing. */ URI apiBaseURLFromEnv() { OptionalURI apiBaseURL = Optional.ofNullable(System.getenv("apiBaseURL")).map(s - { // Compiler error, unhandled URISyntaxException return new URI(s); }); return apiBaseURL.
I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too.
Stay tuned to my blog, twitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.
Java,Functional,Coding
Stay tuned to my blog, twitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.
This article is related to
Java,Functional,Coding
No comments:
Post a Comment