Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is try catch in a view bad practice? [closed]

In an MVC3 application, is it considered bad practice to use a try catch block inside of a razor block @{ } in the .cshtml view?

like image 688
Travis J Avatar asked Mar 23 '12 19:03

Travis J


People also ask

Is try catch best practice?

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)

Can we use try catch in main method?

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.

Can I use try catch inside try catch?

Yes, we can declare a try-catch block within another try-catch block, this is called nested try-catch block.

Does try catch stop execution Javascript?

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) .


2 Answers

Very much so.

Views should not contain any real logic; anything that might throw an exception belongs in the controller.

like image 127
SLaks Avatar answered Sep 21 '22 17:09

SLaks


@{
try
{
    <td>
        @((TradeType)Enum.Parse(typeof(TradeType), item.AppCode)).GetDescription();
    </td>
}
catch
{
    <td>@item.AppCode
    </td>
}
}
like image 34
codeisrun Avatar answered Sep 21 '22 17:09

codeisrun