Feature: 'Safe' methods that guarantee won't throw exception #2866
Replies: 15 comments
-
What would the And what about exceptions that cannot be caught? |
Beta Was this translation helpful? Give feedback.
-
The exception would be returned to the calling method. The return type of the method would be would be something like:
Then it would throw. The try/catch block I normally write would not help in this case either. |
Beta Was this translation helpful? Give feedback.
-
Sounds almost like a constrained execution region in terms of the scenario. But even CERs didn't swallow exceptions, as swallowing exceptions is generally a discouraged pattern since it masks what could very well be a legitimate problem which requires correction. CERs were instead a contract by which a method could be annotated as "I promise not to do anything that could throw a synchronous exception." Out-of-band exceptions (memory corruption or other externalities which we can't account for) could still be fatal to the process. @DouglasHammon What about the scenario where the process itself simply dies or there's a power outage? Does your backend system still need to be resilient against this possibility? And if so, how would the proposed new language feature simplify matters? |
Beta Was this translation helpful? Give feedback.
-
You mean that it can throw, but you'd rather ignore the exception than do anything with it. And that sounds like a really bad pattern to bake into the language or encourage other developers to follow. In these cases bubbling up that something went wrong to the microservice consumer or message queue sounds like exactly what you should be doing. |
Beta Was this translation helpful? Give feedback.
-
This requires static analysis for |
Beta Was this translation helpful? Give feedback.
-
@HaloFour I don't think declaring that a method doesn't throw an exception encourages you to write such methods. If anything, it's just more of a 'statically typed documentation', which I'm a big fan of. Edit: I've reread the proposal and noticed it includes compiler automatically generating |
Beta Was this translation helpful? Give feedback.
-
I agree, appropriate usage, whcih shouldn't be so common that it's onerous to handle exceptions within such methods. Although the BCL methods like |
Beta Was this translation helpful? Give feedback.
-
Precisely, I certainly disagree with OP regarding automatic generation. I initially assumed the proposal was about something similar to |
Beta Was this translation helpful? Give feedback.
-
TryParse work because : Even for a transaction i wouldn't recommend to just ignore the exception. Let's just suppose i agree with you. In my pov i wan't the caller to ignore exception not the method itself. You're method as to do a job and do it right. It is job to throw exception. The caller has the job either to handle them or to send them back to they're own caller. If you wan't to swallow handle
You can also make some neat override.
|
Beta Was this translation helpful? Give feedback.
-
@HaloFour , @GrabYourPitchforks The intent is not to ignore the exception. If the method throws, the exception is returned and the calling method can decided what to do with it (log it, etc..). See the proposed return type here: #2866 (comment) |
Beta Was this translation helpful? Give feedback.
-
So why not use a |
Beta Was this translation helpful? Give feedback.
-
@DouglasHammon from my understanding, what you need is a static analysis for exception handling. Methods need to be analyzed for:
|
Beta Was this translation helpful? Give feedback.
-
@HaloFour Cool. Was not aware of that library. I'll check it out. |
Beta Was this translation helpful? Give feedback.
-
I think that something like C++ noexcept keyword is a better idea. Noexcept method can call only noexcept methods and can't contain throw statement. Of course StackOverflowException still can be thrown but the noexcept feature at least guarantees that the method does something which is safe. |
Beta Was this translation helpful? Give feedback.
-
This is not true: #include <iostream>
#include <exception>
int x() noexcept
{
throw std::runtime_error("oh no");
}
int main()
{
int thing = x();
std::cout << "thing == " << thing << "\n";
} This is valid C++ code. Admittedly, the compiler will likely cause a warning that the noexcept function will always call terminate(). What the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
Often I need to write code where I'm coordinating with multiple external systems (micro-service, queue, email service, payment processing, etc..) in a single unit of work. The code needs to be written in such a way as to prevent an unhandled exception from terminating the execution of process (E.g. I just processed a payment, but now I did to mark the order as 'paid' and it that fails, roll back the payment). It is common when writing this type of process that distributed transactions are not possible.
When writing this code, I will prefix a method name with "Try" to indicate that it won't throw. The implementation of this method wraps its code in a try/catch block.
It would be nice to have a language feature where I could mark a method as being safe (won't throw unhandled exception). This marker could be a [Try] attribute or a keyword similar to async and Task (try keyword with Try result). This attribute/keyword will at compile time wrap the implementation of the method with a try/catch block.
This would eliminate the boiler plate code to wrap these methods in a try/catch block.
What do you think?
Beta Was this translation helpful? Give feedback.
All reactions