How to write a method header that throws exception

依然范特西╮ 提交于 2020-01-24 15:28:36

问题


"Given that FileInputStream’s constructor throws FileNotFoundException, which is a subclass of Exception, write the header for a public method named process that takes a String parameter and returns nothing, and whose body instantiates a FileInputStream object and does not contain a try-catch statement."

I know it's a too-simple question but I want to make sure I'm not messing up in a dumb way. Also, not sure whether to use FileNotFoundException or just Exception or IO, etc.

public process(String file) throws FileNotFoundException {

    FileInputStream file = new FileInputStream("stuff.txt");
}

回答1:


What you have for the throws clause is good. Throwing IOException would not be terrible, but it's better to be specific. Calling code can still treat it as an IOException.

Generally you shouldn't throw Exception except for special cases (Junit methods and similar situations where everything thrown will get caught by an exception handler), because it forces everything that calls it into the position of either handling Exception (where it may not be the appropriate place to do it, it is better to let most exceptions bubble up to one place where they can get handled uniformly) or also throwing Exception, which then puts other calling methods in the same position.

Your method declaration is not valid because there's no return type. Methods that don't return anything are declared with a return type of void.

Using the same name for a method parameter as for a local variable will not work, you should make those different. The constructor call should take the method parameter as an argument (instead of hard-coding a string literal). (+1 to geceo's answer for pointing that one out, I had missed that one.)

As a suggestion, your code would be clearer if it used names that reflected the contents of the variables. Calling a FileInputStream file is not clear, it would be better to call it inputStream. Calling a String file is not clear, it would be better to call it filename.




回答2:


What you have is fine (except you're missing the void return type).

Always throw (and catch) the narrowest specification of Exception you can (in this case FileNotFoundException).




回答3:


As spotted by Nathan, you forgot to declare that process returns nothing, that is, you forgot the void keyword.

There's another problem with your parameter String file ("duplicate local variable file"). You should rename it String filename and pass it to the constructor of FileInputStream:

public void process(String filename) throws FileNotFoundException {
    FileInputStream file = new FileInputStream(filename);
}

About the general topic, you always have to make a choice when you call a method that throws exception(s):

  • You can use a try/catch block and deal with the error in your calling method (log them, for example);

or

  • You can propagate the exception using the throws keyword in your method declaration.

So what you're doing here is the second possibility.




回答4:


Also add void as the return type. Rest is fine.

public void process(String file) throws FileNotFoundException {

    FileInputStream file = new FileInputStream("stuff.txt");
}



回答5:


You can do few things to make your method better by buying insurance against accidental misuse:

public void process(final String file) throws FileNotFoundException {

    final FileInputStream inputStream = new FileInputStream("stuff.txt");
    //invoke operations on inputStream object
    //After 100 lines of code
    inputStream = new FileInputStream("foo.txt"); // compile error
}

Observe the final keyword next to parameter passed to method and FileInputStream object reference.



来源:https://stackoverflow.com/questions/28899425/how-to-write-a-method-header-that-throws-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!