通过Action对象实现任意代码片断的统一异常处理

通过Action对象实现任意代码片断的统一异常处理

这几天对自己写的一个小工具实行重构,发现有程序里要调用很多不同的WCF远程方法,都没有加上try...catch。因为错误处理都几乎相同(弹出消息框),不得不开始了体力劳动,重复地给代码加上相同的try...catch。
写了5次以后,懒人本能开始发作,琢磨起来有没有什么简单的方法能替我实现这一功能。突然想起来前几天写ForAll时候用到的Action对象,眼前看到了曙光。

介绍一下我的方法:
为了能够实现对任何方法的统一try...catch包装,需要通过匿名方法来实现。.NET 3新增加的类型Action,从实质上来说,它就是一个无参数委托。于是写了如下代码
class CommonExceptionProcessor
    {
        public static bool Invoke(Action a, ProcessError p)
        {
            try
            {
                a.Invoke();
                return true;
            }
            catch (Exception ex)
            {
               //......通用错误处理,messagebox.show什么的
 return false;
            }
        }
    }
使用起来这样写
if (!CommonExceptionProcessor.Invoke(
  () => client.SendMediaCommand(command)
  ))
return;
.......

程序功能是完成了,不过这个错误处理也太"通用"了点,稍微改进一下吧。
最终代码:
class CommonExceptionProcessor
    {
        public delegate void ProcessError(Exception ex);
        public static bool Invoke(Action a, ProcessError p)
        {
            try
            {
                a.Invoke();
                return true;
            }
            catch (Exception ex)
            {
                p(ex);
                return false;
            }
        }
    }

调用方法
if (!CommonExceptionProcessor.Invoke(
   () => client.SendMediaCommand(command)
   , pe)) 
//pe是一个弹消息框的委托
return;

这个设计可以很好地实现任意代码的try...catch统一处理。其实Action对象可以实现很多好玩的操作,比如IEnumable的ForAll。
写完了,继续研究偷懒办法去:)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的 Java Web 项目实现登录的统一代码: 1. 创建一个名为 "LoginServlet" 的 Servlet 类,实现用户登录的逻辑: ```java import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); if (username.equals("admin") && password.equals("admin")) { HttpSession session = request.getSession(); session.setAttribute("username", username); response.sendRedirect("welcome.jsp"); } else { response.sendRedirect("login.jsp"); } } } ``` 2. 创建一个名为 "login.jsp" 的 JSP 页面,提供一个登录表单: ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Login Page</title> </head> <body> <form action="LoginServlet" method="post"> <label for="username">Username:</label> <input type="text" id="username" name="username"><br><br> <label for="password">Password:</label> <input type="password" id="password" name="password"><br><br> <input type="submit" value="Login"> </form> </body> </html> ``` 3. 创建一个名为 "welcome.jsp" 的 JSP 页面,用于显示欢迎信息: ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Welcome Page</title> </head> <body> <h1>Welcome <%= session.getAttribute("username") %>!</h1> <a href="logout.jsp">Logout</a> </body> </html> ``` 4. 创建一个名为 "logout.jsp" 的 JSP 页面,用于注销登录: ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Logout Page</title> </head> <body> <h1>You have been logged out!</h1> <a href="login.jsp">Login Again</a> </body> </html> ``` 以上就是一个简单的 Java Web 项目实现登录的统一代码。在实际项目中,可能需要根据具体的需求进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bucher

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值