C
C#2y ago
FroH.LVT

✅ Multiple level try-catch statement

I have a multiple try-catch statement and they behavior differently in different machine.
14 Replies
FroH.LVT
FroH.LVT2y ago
Example code:
void Base()
{
try
{
if (A == true)
{
DoA();
}
else
{
DoB();
}

}
catch
{
// something with it
}
}
void DoB()
{
try
{
// Do something here
}
catch
{
// stuff
}
finally
{
// Clean thing up if error
}
};

Base();
void Base()
{
try
{
if (A == true)
{
DoA();
}
else
{
DoB();
}

}
catch
{
// something with it
}
}
void DoB()
{
try
{
// Do something here
}
catch
{
// stuff
}
finally
{
// Clean thing up if error
}
};

Base();
When I run Base() in my development PC, and let's say it has error then exception is raised, the exception will be on DoB() function. In other machine it's on Base() function; Have you ever met this issue? I'm using .NET Framework 4.8
Becquerel
Becquerel2y ago
why do you think the machine-specific behaviour is to do with try-catch and not the contents of DoA() or DoB()? i don't follow
FroH.LVT
FroH.LVT2y ago
maybe I want it to be handle by DoB alltime though but in other PC it go directly to Base() exception raises at the same line of code
Becquerel
Becquerel2y ago
if you want DoB() to be run for all exceptions that occur in Base() or bubble up to Base(), you need to put it in the catch block of Base() does DoA have its own try-catch internally? maybe it's stopping the exception from bubbling up or something, idk...
FroH.LVT
FroH.LVT2y ago
there are nothing to do with DoA(). I copied Yes, I logged some stuffs in DoB catch. then in finally it will clean state to default, and next operation would be able to run normally I will try to log more information on catch exception
FusedQyou
FusedQyou2y ago
If you catch in DoB and do not rethrow, it will not be caught by Base. If you catch in DoB and then proceed to rethrow, Base will also catch it. If you remove the try-catch from DoB, it will always be caught by Base. This behaviour never changed in any .NET version
FroH.LVT
FroH.LVT2y ago
DoB is in another class Is this mean throw gonna raise exception at highest level? normal exception would go in DoB catch nervousowo issue is due to an exception raised in DoB finally then it goes to Base. Thank for help guys! I kept thinking that DoB finally doesn't have any issues but it does in rare cases
BananaPie
BananaPie2y ago
a bit off topic but I generally do not use try catch to control the flow, try catch is hard to follow, you never know where the exception is truly being caught i use something like FluentResult or OneOf to return the status of the operation
BananaPie
BananaPie2y ago
GitHub
GitHub - altmann/FluentResults: A generalised Result object impleme...
A generalised Result object implementation for .NET/C# - GitHub - altmann/FluentResults: A generalised Result object implementation for .NET/C#
BananaPie
BananaPie2y ago
you do your operation, return a result (a success / failed result) then go from there
FroH.LVT
FroH.LVT2y ago
thank you for sharing, I will try
FusedQyou
FusedQyou2y ago
Doesn't matter. First one to catch it gets to handle it. It's a way of controlling errors that will happen in your application, and in some cases you can throw special exceptions if stuff happends that is not part of your "happy flow" Lol no Exceptions are made for handling undefined behaviour Why would you add a whole wrapper to everything, that's going to cause way more complication Every single method you call now needs an inline switch to ensure it did not return an error, compared to a try-catch which will short circuit the method and define error behaviour in one or more catch blocks This is why try methods exist, return a boolean indicating success and add an out-parameter Suggest you just use try-catch blocks. Note that a try-catch is usually only used to handle actual errors, and you should not throw exceptions when your method works properly.
FroH.LVT
FroH.LVT2y ago
ah yeah, I was confused by other stuffs. I fixed it already
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.