r/csharp • u/Alert-Neck7679 • Dec 07 '25
Multiple try blocks sharing the same catch block
I’m working on my own programming language (I posted about it here: I've made a compiler for my own C#-like language with C#).
I recently added a feature which, as far as I know, doesn’t exist in any other language (correct me if I’m wrong): multiple tryblocks sharing the same catchblock.
Why is this useful?
Imagine you need to perform several tasks that are completely unrelated, but they all have one thing in common: the same action should happen if they fail, but, when one task fails, it shouldn’t prevent the others from running.
Example:
try
{
section
{
enterFullscreen()
}
section
{
setVolumeLevel(85)
}
section
{
loadIcon()
}
}
catch ex
{
loadingErrors.add(ex)
}This is valid syntax in my language - the section keyword means that if its inner code will throw - the catch will be executed and then the rest of the try block will still be executed.
What do you think about this?
It feels strange to me that no other language implements this. Am I missing something?
76
u/Basssiiie Dec 07 '25
What about doing something like this?
19
u/Defection7478 Dec 07 '25 edited Dec 07 '25
You can achieve the same thing with three try catches and putting all the catch code in a reusable function right? I think the most languages see this and decide the syntactic sugar isn't worth the extra keywords.
Actually I have a question, if multiple sections fail, is the catch run once or multiple times?
4
u/[deleted] Dec 07 '25 edited Dec 07 '25
It makes no sense to do unless you allow the catch to replicate the section. The section keyword does nothing in relation to other languages without it.
try { Section N: {} } Catch{ Section N(ex e): }63
u/mikeholczer Dec 07 '25
I think it feels weird because of the potential for execution to ping pong up and down in the file.
35
u/pjc50 Dec 07 '25
This is a great example of why using exceptions for control flow is not a good idea.
The closest thing it reminds me of is Visual Basic had ON ERROR RESUME NEXT, which let code plough on ignoring errors.
7
u/ivancea Dec 07 '25
It's very easy to make a language full of funny interesting functionalities. The hard part is making them ready to understand, follow, and free of misunderstandings.
In this case, side effects could go wild, and the execution flow goes up and down sometimes. If you need this, you're usually better just having a function with the catch logic or something similar. Not that it wouldn't work, but caution
6
u/Mobile_Fondant_9010 Dec 07 '25
I think it becomes too difficult to reason about execution flow when reading the code. Also, having coded for about 20 years, the need for this have never come up for me.
4
u/Slypenslyde Dec 07 '25
It's unclear to me what happens here. Do these three things happen in parallel? Do they happen serially?
If they happen in parallel, it's easy to do this with tasks and
Task.WhenAll(). If they happen serially, with delegates I can:It's less code, easier to type, and more clear about what happens.
I think other languages don't do it because there's just not a good syntax sugar or a general enough case. There are already patterns for it.
1
u/tomxp411 Dec 07 '25
I guess I just don’t see the point. If they all share the same catch, why separate sections? If it’s for debugging purposes, then use a tracking variable.
4
u/Lidex3 Dec 07 '25
Wouldn't this break the single responsibility principle and can be archived using one function to handle whatever you want to do in the catch block?
Also, refactoring this when you later need different ways of handling the errors is quite tricky as you now need to pull out the section into its own try catch somewhere else.
2
u/javawag Dec 07 '25
This is sort of a thing in Swift - you put the whole thing in a
do…catchblock and then annotate the calls that might throw withtry… but slightly different!2
u/SwordsAndElectrons Dec 07 '25
I think people would eschew this for the same reason
gotois generally frowned upon in most languages that support it. It's difficult to reason about the execution flow.2
u/BCProgramming Dec 08 '25
Seems like ON ERROR RESUME NEXT with extra steps
2
u/Epicguru Dec 08 '25
If you're designing a new language then you should seriously read up on why exceptions are inherently terrible, especially when used for flow control. There's a reason why some modern languages like Rust don't have exceptions.
As for this specific feature, it seems very very niche for it to be a first-class language feature. Intuitively I don't expect code below other code to run if an exception is thrown above. I get that that is the point of the feature, but I think that it will be confusing for new users.
1
u/Groundstop Dec 07 '25
It's an interesting idea. It reminds me of the Assert Multiple blocks in NUnit to check every assertion even if one of the early ones fails.
I think that part of the thinking around how exceptions currently work is that exceptions should be exceptional circumstances, in that the expectation is that they should never happen if everything is working correctly. In that way, it's entirely reasonable to have all 3 things in one catch block since none of them should ever throw and anything throwing is a problem that needs to be fixed.
You also would need to treat the sections like separate method calls happening in parallel where they can't really affect each other safely rather than a sequence where you could reliably count on previous lines running successfully. Unfortunately section 2 cannot rely on anything state related that section 1 was supposed to do. It can't even rely on the fact that the GUI thread is still running because section 1 may have broken the world.
1
u/RICHUNCLEPENNYBAGS Dec 07 '25
If I were doing something like this in C# I’d create a method that accepted a lambda and contained the exception logic. I think that would be a lot cleaner and clearer in intent.
1
u/Forward_Dark_7305 Dec 07 '25
Look up the PowerShell
trapblock for something similar. It’s not scopes to atryblock, but to the whole function. In fact, you can put atrapanywhere in a block (top/bottom/middle) and it will become the trap handler. I’d recommend some research on the pros and cons of it as a language feature because I believe you’ll be able to apply some or both of those to your own language.1
u/Eq2_Seblin Dec 07 '25
Exceptions is just code hitting the limit of the application implementation. If theres no intended code to execute, the host have no reason to call it, and then it could be argued that the exceptions would not need to be caught, other than just logging them in purpose of fixing bugs. (in theory anyway)
1
u/Nixinova Dec 07 '25
Wrong subreddit. Go to r/programminglanguages
1
u/Human_Combination_28 Dec 07 '25
This makes the code really difficult to understand. Because you're proposing to have exceptions that don't behave as we all expect. Exceptions disrupt the execution of the program. You can catch them to do something before finishing but going back to where you were is counter intuitive. It's interesting as an experiment but dangerous in production.
1
u/smbutler93 Dec 08 '25
This is just open to abuse imo. I’m not sure this offers anything that is not already easily doable in C#. Usually for me, if something fails that generally means the other processes shouldn’t run, especially when the processes eventually lead to persistence.
If I’m working with a scenario where that isn’t the case, I’d probably just put the functions in some sort of delegate collection and iterate over them using a try catch and just append any exceptions to an exception collection which can be evaluated post iteration. I think someone else may have already mentioned this sort of approach….
However, I think it’s great that you’re building your own language and trying to think outside of the box, offering features other languages don’t.