java - Debug Error: Exception is never thrown in body of corresponding try statement -
i have debug assignment class cannot seem figure out. told debug assignment output assignment looks such.
output:
there problem eagle! java result: 9999
the code given follows:
//superclass custom exception. public class eaglelandingexception extends exception { public eaglelandingexception(string msg) { super(msg); } }
and main class
public class throweagleexceptiontest { public static void main(string[] args) { try { eaglelanding(); } catch (eaglelandingexception badeagle) { system.out.printf("%s\n", badeagle.getmessage()); system.exit(9999); } } private static void eaglelanding () { eaglelandingexception("there problem eagle!"); system.exit(9999); }
i error "the exception never thrown" @ line
catch (eaglelandingexception badeagle)
and receive "cannot find symbol" error on line
eaglelandingexception("there problem eagle!");
i don't understand why happening , have looked @ other questions posted can't seem figure out issue is. thank in advance.
the problem here.
private static void eaglelanding () { eaglelandingexception("there problem eagle!"); system.exit(9999); }
eaglelandingexception(...)
being interpreted call method called eaglelandingexception
. not method. constructor, , name (a class name) not in namespace java looks method names. compilation error (in effect) saying "i cannot find method called ...".
the other problem eaglelanding
method not declared throwing exception. so, when try catch exception in caller, compiler says "this checked exception, , since exception not declared thrown, cannot occur here". hence compilation error.
the correct way write is:
private static void eaglelanding () throws eaglelandingexception { throw new eaglelandingexception("there problem eagle!"); }
notes:
- the
new
causes exception object created - the
throw
causes exception thrown. - the
throws
clause declares method throws checked exception. - statements after
throw
unreachable. if don't remove them, compilation error. (and, besides callingexit
in method bad idea ...) - i gave changed method name conform java style rules. methods should start lowercase name.
in answer, @javaguy suggests eaglelandingexception
should unchecked; i.e. declared subtype of runtimeexception
.
is right? maybe yes, maybe no. in (highly artificial) context have given us, impossible know sure.
however, there general guidelines java designers recommend developers should follow:
checked exceptions intended anticipated events may done exception.
unchecked exceptions intended unanticipated events (e.g. bugs) (typically) there not programmer could do.
the other criterion people use in deciding whether use checked or unchecked exceptions minimize amount of boilerplate code; i.e. throws
clause. lot of people find them annoying, , go considerable lengths avoid them; e.g. declaring custom exceptions unchecked, wrapping standard exceptions in custom exceptions, , on.
i think best path somewhere in between. consider implications of declaring each exception checked or unchecked, , use hierarchy of exceptions manage size of throws
declarations.
Comments
Post a Comment