Вопрос

This may sound silly to some of you experts..but am trying to understand this!

Am trying to implement ELMAH in my MVC Applicaion and suddenly thinking of the question which is exactly asked earlier here Is it okay to use Elmah instead of try/catch?

But i got another question..Why ?

I don use Try catch and elmah logs the exception and user is shown the Error page. so why do i need Try/Catch ?

Application involving SQLTransations in one scenario where i think of try/catch/finally. In other situations can i skip and use only ELMAH

What exactly is the right way ? Hope i get clarified on this :) Thanks

Это было полезно?

Решение

Because without a try/catch, the code stops execution immediately when an exception happens. This can be bad for numerous reasons including:

  • needing to dispose of objects
  • rollback/commit database transactions

There are more, but this gives you a good idea. Try/Catch allows you to trap the exception, choose to handle it or not, then move on with life without crashing your program. Also, who really wants their code to just die because of a NullReferenceException? Even better, what if its an error that you cannot control, such as NetworkTimeout issues, premature HttpConnection closing, etc. It's just good practice to handle your errors where appropriate. This is not meant to say wrap your entire code block in a try/catch!!

If you still want to log to ELMAH even when you catch and handle an exception, you can do something like this:

try {
  ...
}
catch (Exception e) {
  Elmah.ErrorSignal.FromCurrentContext().Raise(e)
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top