Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

java - Is catch(Exception) viable for convenience methods if there is an overload that allows proper handling?

I am developing a library for working with files and want to make it as easy to use (i.e. not having to worry about exceptions), but also as complete as possible (i.e. allow for proper exception handling).

To achieve this, I expose two methods. One that allows for normal catching of exceptions:

public void saveChecked() throws IOException, IllegalArgumentException {
    // Possibly throw an IOException or IllegalArgumentException
}

...and one that handles all exceptions using a generic Consumer<Exception>:

public void save(Consumer<Exception> handler) {
    try {
        saveChecked();
    } catch (Exception exception) {
        handler.accept(exception);
    }
}

The intention behind this is, that, if you don't need to differentiate between exceptions and just want to do one simple thing when an exception is thrown (e.g. print the stack trace, show a popup), you can just use the second version instead of having to write out a try-catch block (and making one-liners impossible).

Is this still bad practice even though the API does expose a method which allows proper exception handling for those that need it?

question from:https://stackoverflow.com/questions/65908014/is-catchexception-viable-for-convenience-methods-if-there-is-an-overload-that

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am developing a library for working with files and want to make it as easy to use (i.e. not having to worry about exceptions), but also as complete as possible (i.e. allow for proper exception handling).

It's java. Exceptions are part of the language. Reinventing the exception system is mostly just going to lead to a bizarre library that doesn't fit in with the rest of java.

do one simple thing when an exception is thrown (e.g. print the stack trace, show a popup)

psv main is allowed to be declared as throws Exception. More generally, if you want to handle all exceptions in the same way, let the exception bubble all the way to the top, and register an exception handler e.g. via Thread.setDefaultUncaughtExceptionHandler.

If you just hate checked exceptions, you probably shouldn't be using java. However, if you somehow must go against the grain, there's always UncheckedIOException that you can throw, which makes that whole 'bubble up to the top, register an uncaught exception handler' a little bit easier.

Is this still bad practice

Yes. Writing non-idiomatic java is bad practice.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...