[
https://issues.apache.org/jira/browse/LUCENE-7796?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15980353#comment-15980353
]
Dawid Weiss commented on LUCENE-7796:
-------------------------------------
bq. Did you know that the Class#newInstance() also throws checked Exceptions
without declaring them!
Yes, I know that. And there is a reason they deprecated it, isn't there? And
while we could use reflection to inspect for unchecked thrown exceptions I
really see little point in doing so -- this is a runtime contract enforcement
that cannot be verified by the compiler. On the other hand, we could simply
declare that attribute instantiation can throw something like
{{AttributeInstantiationException}} which would have a cause initialized to the
underlying throwable and have a clean and portable specification on what
happens if the underlying code throws something unchecked.
bq. If you want to catch all Exceptions in some source code, catch Throwable
and handle based on type + rethrow.
You don't see this as a problem because you're aware of the implementation
details (be it method handles or whatever). I see this as an API inconsistency.
Look again at the code snippet I posted -- how is the (much less experienced
than you) programmer to know that he or she should handle all Throwables, not
just {{Error|RuntimeException}}?
{code}
Analyzer a = new Analyzer() {
@Override
protected TokenStreamComponents createComponents(String fieldName) {
Tokenizer source = new WhitespaceTokenizer();
TokenStream filters = source;
filters = new CustomFilter(source);
return new TokenStreamComponents(source, filters);
}
};
a.tokenStream("", "foo");
{code}
This is a problem in particular if you don't have control over CustomFilter and
don't see that it can throw some kind of unchecked exception. I think some kind
of runtime exception (could be something Lucene-specific -- like the
{{AttributeInstantiationException}} I mentioned) would be a much better fit
here. {{tokenStream}} could even declare it as thrown (even though it's
unchecked) to increase awareness of what can happen at runtime.
What do you think?
> Make reThrow idiom declare RuntimeException return type so callers may use it
> in a way that compiler knows subsequent code is unreachable
> -----------------------------------------------------------------------------------------------------------------------------------------
>
> Key: LUCENE-7796
> URL: https://issues.apache.org/jira/browse/LUCENE-7796
> Project: Lucene - Core
> Issue Type: Improvement
> Reporter: Dawid Weiss
> Assignee: Dawid Weiss
> Priority: Trivial
> Fix For: 6.x, master (7.0)
>
>
> A spinoff from LUCENE-7792: reThrow can be declared to return an unchecked
> exception so that callers can choose to use {{throw reThrow(...)}} as an
> idiom to let the compiler know any subsequent code will be unreachable.
--
This message was sent by Atlassian JIRA
(v6.3.15#6346)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]