Tuesday, 15 January 2013

Java throwing exceptions inside try block -



Java throwing exceptions inside try block -

this question has reply here:

how compare strings in java? 23 answers

i have code looks this:

try { if (resp.equals("a")) { success(resp); } else if (resp.equals("b")) { throw new exception("b error"); } else if (resp.equals("c")) { throw new exception("c error"); } } grab (exception e) { dosomething(e.getmessage()); }

my grab statement doesn't grab error... i'm doing wrong when throw exception gets outside seek block?

none of if-else block executed, because comparing strings using == in of them. in case, try block not throw exception @ all.

use equals method compare string in cases:

if (resp.equals("a"))

or:

if ("a".equals(resp)) // prefer this, don't

the 2nd way avoid npe, avoid using this, since wouldn't know potential exception, , may fall in trap later on.

java exception

No comments:

Post a Comment