C
C#2y ago
Halfbax

❔ Custom "block" statement such as while..do

Hello, is it possible to build a custom block? For example
// usage
DoRequestAsync
{
Categories = await _stockBrowser.GetCategoriesAsync();
}

// code behind DoRequest
private async Task DoRequestAsync()
{
try
{
// children
}
catch (Exception e)
{
Console.WriteLine(e);
Error = true;
}
}
// usage
DoRequestAsync
{
Categories = await _stockBrowser.GetCategoriesAsync();
}

// code behind DoRequest
private async Task DoRequestAsync()
{
try
{
// children
}
catch (Exception e)
{
Console.WriteLine(e);
Error = true;
}
}
9 Replies
Halfbax
Halfbax2y ago
I have a lot of requests that requires a try catch block.
Aaron
Aaron2y ago
no, C# doesn't have a way to add new syntax
Halfbax
Halfbax2y ago
What would be the best way to solve the try catch disaster? Use a try catch around each request?
Aaron
Aaron2y ago
that highly depends on how the rest of your code is structured
Thinker
Thinker2y ago
I mean you could sorta emulate it
await DoRequestAsync(async () =>
{
Categories = await _stockBrowser.GetCategoriesAsync();
}):
await DoRequestAsync(async () =>
{
Categories = await _stockBrowser.GetCategoriesAsync();
}):
Angius
Angius2y ago
The usual way of handling that is to have a global exception handler and let the exceptions bubble up
cptcrawler
cptcrawler2y ago
you can always use ANTLR 😆
demidev_mb
demidev_mb2y ago
Use Polly, it's ultimate library for this kind of things.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
✅ Why am I getting this exception?```cs class algo{ public static void Main(string [] args){ addList.numbers(); i❔ Console.ReadKey() blockingHey there. I have this function that is called every frame: ```public void Update() { if ( !Co❔ Trouble Creating PowerShell CommandletI'm using visual studio 2022, dotnet 6.0. I installed the automation package using > NuGet\Install-P✅ Passing data from controller to viewHi guys hoping for some help here. I have an eCommerce wep app using ASP.NET MVC (7.0) and I want tHow to implement that when you click on the button the data that was sent with it will be displayedHow to implement that when you click on the button the data that was sent with it will be displayed❔ Visual Studio: How to hide tptrace output from Immediate Window during Test Explorer executions?Title says it all. Is there a way to hide the output messages from tptrace that get written to the I❔ Name "PostsService" does not exist in the current context```cs @using BlazorBookGroup.Data . . . private PostsService postsService = new(); protecte❔ System.AccessViolationException: 'Attempted to read or write protected memory.Even with <AllowUnsafeCodeBlocks> enabled and using unsafe { }, this error does not go away. And bef❔ namespace 'Models' does not exist in the namespace 'BlazorBookGroup'It is a subfolder though so what's the issue?❔ .Net Core API User.FindFirstValue(ClaimTypes.NameIdentifier) in every action?!Hi everyone I have the end point below ``` [HttpPost, Route("stores/new"), Authorize(policy: "client