amber-dev Digest, Vol 49, Issue 9
Vikram Bakshi
vab2048 at gmail.com
Thu Mar 25 12:58:13 UTC 2021
> Any ideas how this could be made better? I though about try-catch
expressions,
> but it's still a lot of a code; I've thought about helper method like
> getOrThrow(supplier, exception -> {}), but it's still feels like a half
> solution
There is a feature in the D programming language (and I believe in others)
called 'scope guards'. I have never programmed in D before (Java is my
bread and butter) but I came across it and definitely thought it would be
useful in Java.
Check out the following links explaining it:
1. https://tour.dlang.org/tour/en/gems/scope-guards
2. http://ddili.org/ders/d.en/scope.html
3. https://stackoverflow.com/a/1248484/5108875
In particular the scope(failure) construct is useful in writing more
readable code (as it pertains to exception code).
The stackoverflow post is quite useful because it compares the version with
scope guards with try catch blocks and you can see how it becomes more
readable.
I believe the scope guarded code is actually lowered to try, catch, finally
blocks anyway by the compiler so it is just syntactic sugar.
A pessimist would say they are just smarter 'goto' blocks but even if that
is true I still think they are useful because they increase readability.
Not sure if this actually adds any value to the mailing list but I thought
I would mention it in case...
On Mon, Mar 22, 2021 at 11:55 AM <amber-dev-request at openjdk.java.net> wrote:
> Send amber-dev mailing list submissions to
> amber-dev at openjdk.java.net
>
> To subscribe or unsubscribe via the World Wide Web, visit
> https://mail.openjdk.java.net/mailman/listinfo/amber-dev
> or, via email, send a message with subject or body 'help' to
> amber-dev-request at openjdk.java.net
>
> You can reach the person managing the list at
> amber-dev-owner at openjdk.java.net
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of amber-dev digest..."
>
>
> Today's Topics:
>
> 1. Better exception handling (d3coder)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Mon, 22 Mar 2021 02:20:31 +0600
> From: d3coder <admin at xakeps.dk>
> To: amber-dev at openjdk.java.net
> Subject: Better exception handling
> Message-ID: <2115226.2DT1jp1psY at arch>
> Content-Type: text/plain; charset="us-ascii"
>
> Try-catch expression would be really good feature, to avoid exception
> handling
> bugs, i'm writing code like this:
>
> MyObject myObject;
> try {
> myObject = myObjectFactory.newInstance();
> } catch(IOException e) {
> throw new CustomException("Can't fetch", e);
> }
> myObject.doSomething();
>
> Sometimes i do similar code, but with automatic resource handling, e.g.
> try(InputStream is = openStream())
>
> Any ideas how this could be made better? I though about try-catch
> expressions,
> but it's still a lot of a code; I've thought about helper method like
> getOrThrow(supplier, exception -> {}), but it's still feels like a half
> solution
>
> Maybe language can offer something like ? syntax or would be try-catch
> expressions enough?
>
>
>
>
> End of amber-dev Digest, Vol 49, Issue 9
> ****************************************
>
More information about the amber-dev
mailing list