作者:寓言定格e | 来源:互联网 | 2023-10-13 13:00
我在Java中做了很多…StringsomethingAdefaultvalue.;try{somethingthis.aFunctionThatMightThrowAnExc
我在Java中做了很多…
String something = "A default value.";
try {
something = this.aFunctionThatMightThrowAnException();
} catch (Exception ignore) { }
this.useTheString(something);
现在我正在尝试为std :: string找到一个等价的方法.这是我的……
std::string something("A defualt value.");
try {
something = this->aFunctionThatMightThrowAnException();
} catch (const std::exception& ignore) { }
this->useTheString(something);
为了完整性,这里是aFunctionThatMightThrowAnException()可能看起来像…
std::string MyClass::aFunctionThatMightThrowAnException() {
/* Some code that might throw an std::exception. */
std::string aString("Not the default.");
return aString;
}
我有三个关于C版本的问题:
>这是解决这类问题的方法吗?或者将某些内容作为参考传递给aFunction更常见?
>我的任务是作为从函数返回的东西……安全吗?具体是最初分配给“默认值”的内存.释放?
>在抛出异常的情况下,是否有我无法看到的副作用?
解决方法:
Is this an accepted approach to this kind of problem?
是.
Or is it more common to pass the something into aFunction as a reference?
没有.
Is my assignment to something as the return from aFunction… safe? Specifically is the memory that was originally assigned to “A default value.” released?
是.
Are there side effects I can’t see in the case an exception is thrown?
没有.