简体   繁体   English

尝试捕获异常

[英]try and catch exception

I would like to ask if I'm getting a right syntax in using the try and catch in asp.net我想问一下在 asp.net 中使用 try 和 catch 是否有正确的语法

My code goes like this:我的代码是这样的:

public ActionResult Add_RefPerson(rms_referred_person ref_person)
    {
        if (ModelState.IsValid) {
            try
            {
                ref_person.rf_referreddate = Date();
                ref_person.rf_createdby = getBadge();
                ref_person.rf_updatedby = null;
                ref_person.rf_updateddate = null;
                ref_person.rf_isactive = true;
                db.rms_referred_person.Add(ref_person);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (Exception ex) {
                throw ex;
            }
        }
        return Content("<script type='text/javascript'>alert('Cannot be saved');</script>");
    }

Is my try and catch in the right direction?我的尝试和捕捉方向正确吗? or should I use this one.或者我应该使用这个。

public ActionResult Add_RefPerson(rms_referred_person ref_person)
    {

            try
            {
                   if (ModelState.IsValid) {
                         ref_person.rf_referreddate = Date();
                         ref_person.rf_createdby = getBadge();
                         ref_person.rf_updatedby = null;
                         ref_person.rf_updateddate = null;
                         ref_person.rf_isactive = true;
                        db.rms_referred_person.Add(ref_person);
                        db.SaveChanges();
                        return RedirectToAction("Index");
                }
            }
            catch (Exception ex) {
                throw ex;
            }

        return Content("<script type='text/javascript'>alert('Cannot be saved');</script>");
    }

Thanks a lot.非常感谢。

That is the correct syntax to catch all exceptions;这是捕获所有异常的正确语法; however it is a pretty bad antipattern.然而,这是一个非常糟糕的反模式。 This catches ex and immediately throws it again, clobbering the entire stack trace.这捕获了 ex 并立即再次抛出它,破坏了整个堆栈跟踪。 If rethrowing is desired, write throw;如果需要重新抛出,则写 throw;

In this case you do not want to throw at all, so empty catch might be correct.在这种情况下,您根本不想抛出,因此空捕获可能是正确的。 Consider returning a bit more information about what went wrong, which would require placing the error return in the catch clause itself.考虑返回更多关于出错的信息,这需要将错误返回放在 catch 子句本身中。

The only difference is that the latter one will catch any exceptions in the唯一的区别是后者会在

if (ModelState.IsValid)

line.线。 Unless you think that line might actually throw an exception, they're identical.除非您认为该行实际上可能会引发异常,否则它们是相同的。


You might also want to think more about casting such a wide net for exceptions (ie, narrow down what actual exceptions you want to handle).您可能还想更多地考虑为异常设置如此广泛的网络(即缩小您想要处理的实际异常的范围)。 The idea is to handle what you can and let everything else through for the upper layers to handle.这个想法是处理你能做的,让其他一切通过上层来处理。

In addition, rethrowing an exception is best done with throw on its own rather than throw ex .此外,最好通过throw本身而不是throw ex重新抛出异常。 The former preserves information that the latter will lose.前者保留了后者将丢失的信息。

However, since you're not actually doing anything with the exception other than passing it up the tree, there's little point in catching it in the first place.但是,由于除了将异常传递到树上之外,您实际上没有对异常进行任何操作,因此首先捕获它没有任何意义。 Just execute the commands, if you get an exception, a higher level exception handler should take care of it without you messing around with try..catch .只需执行命令,如果您遇到异常,更高级别的异常处理程序应该会处理它,而无需您使用try..catch

2nd option is safer as it covers ModelState check too.第二个选项更安全,因为它也涵盖了ModelState检查。 Also, throw ex;另外, throw ex; is not a great idea.不是个好主意。 you will not get complete stack trace.你不会得到完整的堆栈跟踪。 use throw;使用throw;

         try
            {
                   if (ModelState.IsValid) {
                         ref_person.rf_referreddate = Date();
                         ref_person.rf_createdby = getBadge();
                         ref_person.rf_updatedby = null;
                         ref_person.rf_updateddate = null;
                         ref_person.rf_isactive = true;
                        db.rms_referred_person.Add(ref_person);
                        db.SaveChanges();
                        return RedirectToAction("Index");
                }
            }
            catch (Exception ex) {
                throw ex;
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM