java - Remove repetitive try/catch code -


i have following code in many methods

try{ user = userlist.get(user_id); } catch(exception e) { return "user_not_found"; } 

i find self using code quite lot in different methods, remove repedidnes try create method this: (example)

.. returnuser(){      try{ return user = userlist.get(user_id); }     catch(exception e) { return "user_not_found"; } } 

this return user or string , not brake out of medthod if user not found: (not working)

public static void main(string[] args){     system.out.pring("this run always");     user = returnuser();     system.out.pring("this should run if user returned");     system.out.pring("otherwise 'user_not_found' should returned , end app.");     ....     ....     return "succsess";  } 

is there better way this? can return brake out of method , return string , end iditial method?

code example:

public string completetodo(){      todo todo; // instantiate user object     user user; // instantiate todo object      try{ todo = todolist.get(user_id); } //repeditive in functions     catch(exception e) { return "todo_not_found"; }      try{ user = userlist.get(user_id); } //repeditive in functions     catch(exception e) { return "user_not_found"; }      if(user.gettoken() == token){                   user.setdef(1);                  return user.tostring();          }      return "user_not_valid";  } 

a function returns user

public user returnuser() throws usernotfoundexception {      try{          return user = userlist.get(user_id);     }     catch (exception e) {         throw new usernotfoundexception ("user " + user + " not found");     } } 

main method

public static void main(string... args){      try {         system.out.println ("this run always");   // not work because returnuser() not static method , there no "this" in "static void main()"         user user = this.returnuser();          system.out.println ("this should run if user returned");         return "success";      } catch (usernotfoundexception e) {         system.out.pring("otherwise 'user_not_found' should returned , end app.");     } 

exception classes

class appexception extends exception {     public appexception(string msg) {          super(msg);     } }  class usernotfoundexception extends appexception {     public usernotfoundexception(string msg) {          super(msg);     } } 

upd

here mean data service, code compilable

import java.util.arraylist; import java.util.list;  public class app {      public static void main(string... args) {          app app = new app();          try {             app.run();          } catch (usernotfoundexception e) {             system.out.println("'user_not_found'");         } catch (todonotfoundexception e) {             system.out.println("'todo_not_found'");         } catch (appexception e) {             system.out.println("some other application exception");         }     }      dataservice dataservice = null;      public void run() throws appexception {          system.out.println("this run always");          preparedataservice();          completetodo(1, 2);          system.out.println("this should run if user returned");      }      void preparedataservice() {          list<todo> todolist = new arraylist<todo>();         list<user> userlist = new arraylist<user>();          dataservice = new dataservice(todolist, userlist);     }      void completetodo(int todoid, int userid) throws appexception {          todo todo = dataservice.findtodo(todoid);         user user = dataservice.finduser(userid);          user.dosomething(todo);     } }  class dataservice {      private list<todo> todolist;     private list<user> userlist;      public dataservice(list<todo> todolist, list<user> userlist) {         this.todolist = todolist;         this.userlist = userlist;     }      public todo findtodo(int todoid) throws todonotfoundexception {         todo todo = null;          // find todo here          if (todo == null) {             throw new todonotfoundexception("todo " + todo + " not found");         }         return todo;     }      public user finduser(int userid) throws usernotfoundexception {         user user = null;          // find user here          if (user == null) {             throw new usernotfoundexception("user " + user + " not found");         }         return user;     } }  class todo { }  class user {      public void dosomething(todo todo) {     }; }  class appexception extends exception {     public appexception(string msg) {         super(msg);     } }  class todonotfoundexception extends appexception {     public todonotfoundexception(string msg) {         super(msg);     } }  class usernotfoundexception extends appexception {     public usernotfoundexception(string msg) {         super(msg);     } } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -