Java 删除重复的try/catch代码
我在许多方法中都有以下代码Java 删除重复的try/catch代码,java,web-services,methods,try-catch,Java,Web Services,Methods,Try Catch,我在许多方法中都有以下代码 try{ user = userList.get(user_id); } catch(Exception e) { return "USER_NOT_FOUND"; } 我发现我自己在不同的方法中使用了很多代码,为了消除重复,我可以尝试创建如下方法:(示例) 然而,这只会返回一个用户或字符串,如果找不到用户,则不会退出medthod:(不工作) 有更好的方法吗?你能不能从方法中返回一个brake,然后返回字符串并结束idtial方法 代码示例: public Str
try{ user = userList.get(user_id); }
catch(Exception e) { return "USER_NOT_FOUND"; }
我发现我自己在不同的方法中使用了很多代码,为了消除重复,我可以尝试创建如下方法:(示例)
然而,这只会返回一个用户或字符串,如果找不到用户,则不会退出medthod:(不工作)
有更好的方法吗?你能不能从方法中返回一个brake,然后返回字符串并结束idtial方法
代码示例:
public String completeTodo(){
Todo todo; // Instantiate User object
User user; // Instantiate todo object
try{ todo = todoList.get(user_id); } //repeditive in all functions
catch(Exception e) { return "TODO_NOT_FOUND"; }
try{ user = userList.get(user_id); } //repeditive in some functions
catch(Exception e) { return "USER_NOT_FOUND"; }
if(user.getToken() == token){
user.setDef(1);
return user.toString();
}
return "USER_NOT_VALID";
}
返回用户的函数
public User returnUser() throws UserNotFoundException {
try{
return user = userList.get(user_id);
}
catch (Exception e) {
throw new UserNotFoundException ("user " + user + " not found");
}
}
主要方法
public static void main(String... args){
try {
System.out.println ("This will run always");
// this will not work because returnUser() is not a static method and there is no "this" in "static void main()"
User user = this.returnUser();
System.out.println ("this should only run if a user was returned");
return "Success";
} catch (UserNotFoundException e) {
System.out.pring("otherwise 'USER_NOT_FOUND' should be returned and end app.");
}
异常类
class AppException extends Exception {
public AppException(String msg) {
super(msg);
}
}
class UserNotFoundException extends AppException {
public UserNotFoundException(String msg) {
super(msg);
}
}
UPD
这就是我对数据服务的意思,代码是可编译的
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 will run always");
prepareDataService();
completeTodo(1, 2);
System.out.println("this should only run if a user was 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);
}
}
import java.util.ArrayList;
导入java.util.List;
公共类应用程序{
公共静态void main(字符串…参数){
App App=新App();
试一试{
app.run();
}catch(usernotfounde异常){
System.out.println(“'USER\u NOT\u FOUND'”);
}catch(todonotfounde异常){
System.out.println(“'TODO\u NOT\u FOUND'”);
}捕获(外观e){
System.out.println(“某些其他应用程序异常”);
}
}
DataService DataService=null;
public void run()引发AppException{
System.out.println(“这将始终运行”);
prepareDataService();
复合毒素(1,2);
System.out.println(“仅当返回用户时才应运行此命令”);
}
作废预处理服务(){
List todoList=new ArrayList();
List userList=new ArrayList();
数据服务=新的数据服务(todoList、userList);
}
void completeTodo(int-todoId,int-userId)抛出AppException{
Todo Todo=dataService.findTodo(todoId);
User=dataService.findUser(userId);
用户剂量测量(todo);
}
}
类数据服务{
私人名单;
私有列表用户列表;
公共数据服务(列表到列表,列表用户列表){
this.todoList=todoList;
this.userList=userList;
}
公共Todo findTodo(int todoId)抛出TodoNotFoundException{
Todo Todo=null;
//在这里找到要做的事
如果(todo==null){
抛出新的TodoNotFoundException(“todo”+todo+“未找到”);
}
返回待办事项;
}
公共用户findUser(int userId)抛出UserNotFoundException{
User=null;
//在此处查找用户
if(user==null){
抛出新的UserNotFoundException(“用户”+用户+“未找到”);
}
返回用户;
}
}
待办事项{
}
类用户{
公共无效剂量测定(Todo Todo){
};
}
类AppException扩展了异常{
公共AppException(字符串msg){
超级(味精);
}
}
类TodoNotFoundException扩展AppException{
公共TodoNotFoundException(字符串消息){
超级(味精);
}
}
类UserNotFoundException扩展AppException{
公共UserNotFoundException(字符串消息){
超级(味精);
}
}
返回用户的函数
public User returnUser() throws UserNotFoundException {
try{
return user = userList.get(user_id);
}
catch (Exception e) {
throw new UserNotFoundException ("user " + user + " not found");
}
}
主要方法
public static void main(String... args){
try {
System.out.println ("This will run always");
// this will not work because returnUser() is not a static method and there is no "this" in "static void main()"
User user = this.returnUser();
System.out.println ("this should only run if a user was returned");
return "Success";
} catch (UserNotFoundException e) {
System.out.pring("otherwise 'USER_NOT_FOUND' should be returned and end app.");
}
异常类
class AppException extends Exception {
public AppException(String msg) {
super(msg);
}
}
class UserNotFoundException extends AppException {
public UserNotFoundException(String msg) {
super(msg);
}
}
UPD
这就是我对数据服务的意思,代码是可编译的
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 will run always");
prepareDataService();
completeTodo(1, 2);
System.out.println("this should only run if a user was 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);
}
}
import java.util.ArrayList;
导入java.util.List;
公共类应用程序{
公共静态void main(字符串…参数){
App App=新App();
试一试{
app.run();
}catch(usernotfounde异常){
System.out.println(“'USER\u NOT\u FOUND'”);
}catch(todonotfounde异常){
System.out.println(“'TODO\u NOT\u FOUND'”);
}捕获(外观e){
System.out.println(“某些其他应用程序异常”);
}
}
DataService DataService=null;
public void run()引发AppException{
System.out.println(“这将始终运行”);
prepareDataService();
复合毒素(1,2);
System.out.println(“仅当返回用户时才应运行此命令”);
}
作废预处理服务(){
List todoList=new ArrayList();
List userList=new ArrayList();
数据服务=新的数据服务(todoList、userList);
}
void completeTodo(int-todoId,int-userId)抛出AppException{
Todo Todo=dataService.findTodo(todoId);
User=dataService.findUser(userId);
用户剂量测量(todo);
}
}
类数据服务{
私人名单;
私有列表用户列表;
公共数据服务(列表到列表,列表用户列表){
this.todoList=todoList;
this.userList=userList;
}
公共Todo findTodo(int todoId)抛出TodoNotFoundException{
Todo Todo=null;
//在这里找到要做的事
如果(todo==null){
抛出新的TodoNotFoundException(“todo”+todo+“未找到”);
}
返回待办事项;
}
公共用户findUser(int userId)抛出UserNotFoundException{
User=null;
//在此处查找用户
if(user==null){
抛出新的UserNotFoundException(“用户”+用户+“未找到”);
}
返回用户;
}
}
待办事项{
}
类用户{
公共无效剂量测定(Todo Todo){
};
}
类AppException扩展了异常{
公共AppException(字符串msg){
超级(味精);
}
}
类TodoNotFoundException扩展AppException{
公共TodoNotFoundException(字符串消息){
超级(味精);
}
}
类UserNotFoundException扩展AppException{
公共UserNotFoundException(字符串消息){
超级(味精);
}
}
我会这样编写您的todo方法:
public String completeTodo() throws SpecificExceptionHere {
Todo todo = todoList.get(user_id);
User user = userList.get(user_id);
if (user.getToken() == token){
user.setDef(1);
return user.toString();
} else {
throw new AppropriateException("Invalid token: " + user.getToken());
}
}
并将异常处理留给调用代码。我会这样编写您的todo方法:
public String completeTodo() throws SpecificExceptionHere {
Todo todo = todoList.get(user_id);
User user = userList.get(user_id);
if (user.getToken() == token){
user.setDef(1);
return user.toString();
} else {
throw new AppropriateException("Invalid token: " + user.getToken());
}
}
并将异常处理留给调用代码。如何向我们展示您真正的代码和方法?这些代码看起来都不太正确,我很怀疑它们是否真的被编译过。在Java中不能返回用户或字符串。除非使用returntype对象,否则w