Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove 'throws Exception' from Pointer::close #86

Merged
merged 1 commit into from
Mar 15, 2016

Conversation

osialr
Copy link
Contributor

@osialr osialr commented Mar 11, 2016

Pointer throws a RuntimeException on close. Removing the throws
allows the user's try-with-resource blocks not require a catch(Exception)

Pointer throws a RuntimeException on close.  Removing the throws
allows the user's try-with-resource blocks not require a catch(Exception)
@saudet
Copy link
Member

saudet commented Mar 12, 2016

I've thought about that myself, but if we do that, then it becomes impossible to override the close() method with a checked exception, like IOException, so everything would need to be wrapped in a RuntimeException or something. Is this really wise?

On the other hand, it is possible to override the close() method to get rid of the throws Exception, so depending on the need of each class, it seemed to me that this way the user was less limited, for example:

public NoThrowClass extends Pointer {
    @Override public void close() { super.close(); }
}
public CustomThrowClass extends Pointer {
    @Override public void close() throws CustomException { super.close(); }
}

@osialr
Copy link
Contributor Author

osialr commented Mar 12, 2016

I see your point and that adding this could be a breaking change. My reasoning was that deallocate() can't throw Exception so I propagated that same attribute up to close()

A custom class could override close() to remove the throws. But the auto-generated are still stuck with Exception.

The Parser could add a throw-less close to each autogenerated struct:

@Override
void close() { 
    try {
        super.close();
    } catch (Exception e) { 
       throw new RuntimeException(e);
    }
}

@saudet
Copy link
Member

saudet commented Mar 12, 2016

Right, super.close() throws, so actually:

@Override public void close() { deallocate(); }

Not so bad if we needed it I thought.

@saudet
Copy link
Member

saudet commented Mar 13, 2016

It's a bit like the case of the position(int position) method. It obviously can't return anything but itself, in the type of the current class, but we have to add a line in the subclass to cast it explicitly...

@osialr
Copy link
Contributor Author

osialr commented Mar 13, 2016

Well not calling super.close() is bad form in the general case. However, I will try to work something out to go along with #86 for C-struct based classes. I think my usages can be solved adding this.

@saudet
Copy link
Member

saudet commented Mar 15, 2016

I've given this some more thoughts. Since Pointer.deallocate() via close() is meant to execute a C++ destructor, we shouldn't be seeing exceptions thrown from there. Users that want to wrap something like std::fstream::close() that does throw exceptions, can still do so via some higher-level wrapper class in Java. So, let's make Pointer simpler to use. Thanks for bringing this up!

saudet added a commit that referenced this pull request Mar 15, 2016
 * Remove `throws Exception` from `Pointer.close()`
@saudet saudet merged commit 4f99a94 into bytedeco:master Mar 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants