✅ Multiple level try-catch statement
I have a multiple try-catch statement and they behavior differently in different machine.
14 Replies
Example code:
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
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
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
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...
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
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 versionDoB is in another class
Is this mean throw gonna raise exception at highest level?
normal exception would go in DoB catch 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
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
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#
you do your operation, return a result (a success / failed result) then go from there
thank you for sharing, I will try
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.
ah yeah, I was confused by other stuffs. I fixed it already
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.