使用strerror方法的时机

huangapple go评论132阅读模式
英文:

When to use strerror method

问题

我最近发现了这段遗留代码,它使用strerror()方法来查找文件操作失败的原因,而不是使用异常对象。
在一个场景中,硬盘已满导致文件复制失败,strerror()返回“无错误”。异常对象提供了正确的“磁盘上没有足够的空间”错误。

在对代码进行任何更改之前,我想了解strerror。
1. 它只注册C运行时库中的错误到strerror中吗?
2. 有没有情况下,strerror会提供比例如文件系统异常更好的错误消息?
3. 如果我们没有直接调用任何C运行时函数,我们有使用strerror()的理由吗?

在Windows和Linux上使用boost 1.71

感谢任何建议。
英文:

I recently found this legacy code that uses strerror() method to find out why a file operation failed instead of using the exception object.

try
{
    boost::filesystem::path src(...);
    boost::filesystem::path dst(...);
    boost::filesystem::copy_file(src, dst);
}
catch (const boost::filesystem::filesystem_error& ex)
{
   std::cout << "Failed to copy files. " << strerror(errno) << std::endl;
}

In one scenario the hard disk was full which caused the file copy to fail and strerror() returned "No Error". The exception object provided a correct "There is not enough space on the disk" error.

Before making any changes to the code I wanted to understand strerror.

  1. Is it "only" C runtime libraries that register errors "in" strerror?
  2. Are there any scenarios where strerror will provide a better error message than for example a filesystem exception?
  3. If we are not calling any C runtime functions directly, do we have any reason to use strerror()?

Using boost 1.71 on Windows and Linux

Appreciate any input

答案1

得分: 1

copy_file有两种重载:

  • 一个会抛出异常并且不带错误代码参数
bool copy_file(const path& from, const path& to);
  • 一个不会抛出异常并且接受一个错误代码参数
bool copy_file(const path& from, const path& to, system::error_code& ec);

这适用于boost标准库的实现。在你提供的代码片段中,没有将error_code信息传递给调用,因此所有错误报告都应该使用异常对象进行。

不过,针对具体的问题:

> 1. 仅仅是C运行时库会在strerror中注册错误吗?

不是。标准库版本就是一个例子:

"接受std::error_code&参数的重载会在OS API调用失败时将其设置为OS API错误代码,并且如果没有发生错误,则执行ec.clear()。"

> 2. 是否存在某些情况下,strerror提供的错误消息会比文件系统异常更好?

几乎没有,因为异常消息本质上是“将from作为第一个路径参数,to作为第二个路径参数,将OS错误代码作为错误代码参数的连接”。

> 3. 如果我们没有直接调用任何C运行时函数,有没有理由使用strerror()?

这不是关于“消息丰富度”的问题。有一些与线程安全性相关的影响(请阅读提供的链接),但非抛出版本应该用于不适合使用异常的情况,例如:

  • 受限制的环境,
  • 被禁止使用(由于编码标准或公司政策),
  • 现有代码库不使用异常,而您不想违反这一规则,
  • 平台或应用程序的操作使得复制失败的概率足够高,以至于不被视为异常情况。
英文:

copy_filehas two overloads:

  • One that throws and doesn't take an error code parameter
bool copy_file(const path& from, const path& to);
  • One that does not throw and accepts an error code parameter
bool copy_file(const path& from, const path& to, system::error_code& ec);

This applies both to the boost and standard library implementations. In the snippet you provide, no error_code information is passed to the call so all error reporting should be done using the exception object.

That said, to address specific questions:

> 1. Is it "only" C runtime libraries that register errors "in" strerror?

No. Case in point, for the standard library version:

"The overload taking a std::error_code& parameter sets it to the OS API error code if an OS API call fails, and executes ec.clear() if no errors occur."

> 2. Are there any scenarios where strerror will provide a better error message than for example a filesystem exception?

Hardly, because the exception message is essentially a concatenation of "from as the first path argument, to as the second path argument, and the OS error code as the error code argument".

> 3. If we are not calling any C runtime functions directly, do we have any reason to use strerror()?

It's not about "message richness". There are some implications with thread safety (read up on the provided links) but the non throwing versions are to be used where exceptions are not a fitting choice e.g.:

  • restricted environments,
  • banned from usage (due to coding standard or company policy)
  • the existing codebase does not use exceptions and you don't want to code against the grain
  • Operation of the platform or application makes copy failure probability high enough to not be considered an exceptional case.

huangapple
  • 本文由 发表于 2023年6月30日 00:38:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583014.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定