java - Exception is never thrown in body of corresponding try statement -
i have problem exception handling in java, here's code. got compiler error when try run line: throw new mojexception("bledne dane");
. error is:
exception mojexception never thrown in body of corresponding try statement
here code:
public class test { public static void main(string[] args) throws mojexception { // todo auto-generated method stub for(int i=1;i<args.length;i++){ try{ integer.parseint(args[i-1]); } catch(mojexception e){ throw new mojexception("bledne dane"); } try{ wiersztrojkatapascala = new wiersztrojkatapascala(integer.parseint(args[0])); system.out.println(args[i]+" : "+a.wspolczynnik(integer.parseint(args[i]))); } catch(mojexception e){ throw new mojexception(args[i]+" "+e.getmessage()); } } } }
and here code of mojexception:
public class mojexception extends exception{ mojexception(string s){ super(s); } }
can me this?
a catch-block in try statement needs catch exactly exception code inside try {}
-block can throw (or super class of that).
try { //do throws exceptiona, e.g. throw new exceptiona("i exception alpha!"); } catch(exceptiona e) { //do handle exception, e.g. system.out.println("message: " + e.getmessage()); }
what trying this:
try { throw new exceptionb("i exception bravo!"); } catch(exceptiona e) { system.out.println("message: " + e.getmessage()); }
this lead compiler error, because java knows trying catch exception never ever ever occur. get: exception exceptiona never thrown in body of corresponding try statement
.
Comments
Post a Comment