In an MVC3 application, is it considered bad practice to use a try catch block inside of a razor block @{ }
in the .cshtml
view?
It's considered best practice to put as little code as possible in each try / catch block. This means that you may need multiple try / catch blocks, instead of just one. The benefits of this are that: it's easy to see which code raises which exceptions (and which code doesn't raise exceptions)
It must be used within the method. If an exception occurs at the particular statement in the try block, the rest of the block code will not execute. So, it is recommended not to keep the code in try block that will not throw an exception. Java try block must be followed by either catch or finally block.
Yes, we can declare a try-catch block within another try-catch block, this is called nested try-catch block.
The “try… If there were no errors, then catch (err) is ignored: the execution reaches the end of try and goes on, skipping catch . If an error occurs, then the try execution is stopped, and control flows to the beginning of catch (err) .
Very much so.
Views should not contain any real logic; anything that might throw an exception belongs in the controller.
@{
try
{
<td>
@((TradeType)Enum.Parse(typeof(TradeType), item.AppCode)).GetDescription();
</td>
}
catch
{
<td>@item.AppCode
</td>
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With