I want to disable the warning of my catch statement, but only of the catch statement! The same way as in the .cs file!
Important! : I do not want to remove the SystemException e argument! My specific demand is to specifically suppress the waring of the catch statement with the Razor syntax. Same way as it works in a .cs file without razor.
Initial situation:
<div>
@{
try
{
//some code
<div>yes</div>
}
catch (SystemException e)
{
<div>error</div>
}
}
</div>
I get:
The variable 'e' is declared but never used
a) If I put the pragma
statements around the catch
, I get following error:
#pragma warning disable 0168
catch (SystemException e)
#pragma warning restore 0168
Error 11 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement _Header.cshtml 72 13
b) If I put a Razor-@
before the <div>
block, I get following error:
@<div>error</div>
Error 5 ; expected _Header.cshtml 1 1
c) If I put a ;
after the <div>
block, it seems OK, but when loading the site I get following compilation error:
@<div>error</div>;
Compilation error:
Line 379: #line default
Line 380: #line hidden
Line 381:item => new System.Web.WebPages.HelperResult(__razor_template_writer => {
Line 382:
Line 383:BeginContext(__razor_template_writer, "~/Views/Shared/_Header.cshtml", 1947, 16, true);
If you are never using the catch exception, you could just do this:
<div>
@{
try
{
//some code
<div>yes</div>
}
catch
{
<div>error</div>
}
}
</div>
Whilst I think wrapping pragmas around your catch simply to ignore a warning about an unused variable is, frankly, a hideous proposal (not only does it look ugly, but I think it reduces readability, and confuses matters - are you using the variable or not?), if you really need to do that, try this:
<div>
@{
try
{
//some code
throw new SystemException();
<div>yes</div>
}
catch
(
#pragma warning disable 0168
SystemException e
#pragma warning restore 0168
)
{
<div>error</div>
}
}
</div>