首页 新闻 会员 周边

关于web页面中try catch的问题

0
悬赏园豆:50 [已关闭问题] 解决于 2013-07-26 15:10

最近无聊, 在看微软的示例PetShopV4, 虽然看到很多次, 每次看都有不同的收获。今天看了一些内容有一些想法需要各大侠来解答:

我平时个人在开发项目的时候会在一个事件的方法下做try catch,同时在catch里面添加txt/xml的log记录并提示。但今天看了PetShopV4,用这种方法对比一下, 发现他们没有这用的处理, 基本上都是直接代码到底,用web.config指定customErrors的指向。

像insert order的过程:

// create order
OrderInfo order = new OrderInfo(int.MinValue, DateTime.Now, User.Identity.Name, GetCreditCardInfo(), billingForm.Address, shippingForm.Address, Profile.ShoppingCart.Total, Profile.ShoppingCart.GetOrderLineItems(), null);

// insert
Order newOrder = new Order();
newOrder.Insert(order);

处理的部分过程

/// <summary>
        /// A method to insert a new order into the system
        /// As part of the order creation the inventory will be reduced by the quantity ordered
        /// </summary>
        /// <param name="order">All the information about the order</param>
        public void Insert(OrderInfo order)
        {

            // Call credit card procesor
            ProcessCreditCard(order);

            // Insert the order (a)synchrounously based on configuration
            orderInsertStrategy.Insert(order);
        }

        /// <summary>
        /// Process credit card and get authorization number. 
        /// </summary>
        /// <param name="order">Order object, containing credit card information, total, etc.</param>
        private void ProcessCreditCard(OrderInfo order)
        {

            // In the real life environment here should be a call to the credit card processor API.
            // We simulate credit card processing and generate an authorization number.
            Random rnd = new Random();
            order.AuthorizationNumber = (int)(rnd.NextDouble() * int.MaxValue);

            // Check if authorisation succeded
            if (!order.AuthorizationNumber.HasValue)
                throw new ApplicationException(CREDIT_CARD_ERROR_MSG);
        }

 

如果中途真的出现 throw new ApplicationException这事件,只能指向customErrors? 而不是把错误提交给用户,通知用户重新操作?

如果把只是一个示例是这样我也不怀疑,但在Codematic李天平老师的生成器里面, 生成出来的页页ADD事件也是没有try catch,难道就一定能保证添加插入事件100%成功, 或者定向认定失败后,直接指向customErrors?

有下列问题:

1、try catch在这些大作中都没用到,是不是性能影响真的很大?

2、如果没有try catch,真的出现失误时,怎对系统进行跟踪,对出现异常进行跟踪?

3、各大侠,是怎处理的呢? 求答案,谢谢.

风浪的主页 风浪 | 老鸟四级 | 园豆:2996
提问于:2013-07-23 17:14
< >
分享
其他回答(1)
0

异常最终肯定是要有一个地方处理的,你可以看一下,微软企业库异常处理模块,它告诉你怎么处理异常更合适。

小兵仔 | 园豆:1240 (小虾三级) | 2013-07-23 17:34

这个异常我知道怎处理, 我只想知道像这些大作为什么不用try catch, 还是直接指向customErrors好?还是try catch捉到提示记录好?

支持(0) 反对(0) 风浪 | 园豆:2996 (老鸟四级) | 2013-07-23 17:46
0

只要不出现异常的情况下不会有什么性能损失的。

我为球狂 | 园豆:5 (初学一级) | 2013-07-24 09:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册