java - Can I miss the catch clause to throw exception to its caller? -
what kind of problem may occur code? think exception occurs, code can throw exception caller. not generate trouble.
how can improve it?
public static void cat(file named) { randomaccessfile input = null; string line = null; try { input = new randomaccessfile(named, “r”); while ((line = input.readline()) != null { system.out.println(line); } return; } { if (input != null) { input.close(); } } }
what kind of problem may occur code?
- public randomaccessfile
throws filenotfoundexception
. - public final string readline()
throws ioexception
. - public void close()
throws ioexception
.
since public class filenotfoundexception extends ioexception
you can change method to:
public static void cat(file named) throws ioexception
and don't need try-catch blocks.
and caller should catch exception
thrown method.
but why don't want catch
exceptions?
Comments
Post a Comment