java - Using try-catch -- Skipping try -
im trying write code can try json object , if wrong format inform user somehow.
the code :
public boolean sjekksporingsnummer (jsonobject object){ //object passed correct boolean riktigsporing = null; riktigsporing = true; //riktig sporing set true //if true json correct try { jsonarray consignmentset = object.getjsonarray("consignmentset"); jsonobject object1 = consignmentset.getjsonobject(0); riktigsporing = true; }catch (exception e){ //skips straigt here e.printstacktrace(); riktigsporing = false; } return riktigsporing;
after if failes :
07-31 12:34:07.243 15479-15479/com.example.posten e/androidruntime: fatal exception: main java.lang.nullpointerexception
what seems wierd me app skips try , goes straight return statement.
i try , of failes set "riktigsporing" false.
what doing wrong ?
most exception @ first line in try block.
i think there better approach code method. not use boolean show how successful method execution. if method executes ok, return nothing. if there's error upon execution, throw exception specific error have got.
avoid use of general exception classes possible. use exception classes specific sitiation caused problem. i.e nullpointerexception
, jsonexception
, numberformatexception
, on. if there's custom specific possible reason or reasons exceptions make custom exception class , throw when necessary mark problem precise possible.
it let caller process error or throw exception further.
in general approach makes application more consistent , manageable.
Comments
Post a Comment