C++ 0x有两个预定义的error_category对象:generic_category()和system_category().从我到目前为止所理解的,system_category()应该用于操作系统返回的错误,并且generic_category()应该用于找到的通用值std::errc,它们对应于errno值.
但是,在类Unix系统上应该做些什么,其中errno值是操作系统返回的错误?我应该使用system_category()(在非类Unix系统上会出错,需要#ifdef),还是应该使用generic_category()(对于非标准errno值,类Unix系统会出错)?
C++ 中已经有足够多的错误处理策略。我们有异常处理、错误返回码和这些ERRNO乱七八糟的东西。什么样的角色在system_error这里头玩?我如何使用那里的功能?对我来说,它看起来只是随机组合在一起。我使用cppreference站点作为参考。
虽然我正在使用C++ 11,但这个问题与提升相关,因为我正在处理错误boost::file_system.
在以下情况中:
try {
// If p2 doesn't exists, canonical throws an exception
// of No_such_file_or_directory
path p = canonical(p2);
// Other code
} catch (filesystem_error& e) {
if (e is the no_such_file_or_directory exception)
custom_message(e);
} // other catchs
}
Run Code Online (Sandbox Code Playgroud)
如果我在抛出所需的异常(no_such_file_or_directory)时打印错误值:
// ...
} catch (filesystem_error& e) {
cout << "Value: " << e.code().value() << endl;
}
Run Code Online (Sandbox Code Playgroud)
我得到了价值2.它的价值是相同的e.code().default_error_condition().value().
我的问题是:不同错误类别的不同错误条件是否具有相同的值?我的意思是,我是否需要同时检查错误类别和错误值,以确保我收到特定错误?在这种情况下,最干净的方法是什么?
所以我在 Visual Studio 2015 中使用 SFML 编写了一个简单的图形蛇游戏,它在我的主计算机上完美运行。我想我应该在我的笔记本电脑上尝试一下。运行程序时,它给了我这个错误: 系统错误:程序无法启动,因为您的计算机缺少 MSVCP140D.DLL。尝试重新安装程序来解决这个问题 所以我在我的电脑中搜索它并找到它所以我将它复制到我的笔记本电脑上,然后我再次收到另一个错误: 应用程序错误:应用程序无法正确启动(0xc000007b)。单击确定关闭应用程序。 我尝试重新安装 Microsoft Visual C++ Redistributable,但仍然无法正常工作。(顺便说一句,这不是代码问题,我已经正确安装了 SFML 并使用了它的库和 bin 没有任何问题)。你的帮助对我来说意义重大。谢谢!这是我的代码:
//
GraphicalLoopSnakeGame.cpp :
Defines the entry point for
the console application.
//
#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include <time.h>
using namespace sf;
int N = 30, M = 20;
int size = 16;
int w = size*N;
int h = size*M;
int dir, num = 4;
struct Snake
{
int x, y;
} s[100];
struct Fruit
{
int x, y;
} …Run Code Online (Sandbox Code Playgroud) 查看 Node 的文档,SystemError扩展了该类Error。然而,如果你尝试直接使用SystemError,Node 会抛出ReferenceError这样的信息:SystemError。
显然,根据文档,有可能遇到 的实例SystemError,但似乎不可能以通常的方式重新创建此类错误(例如throw new SystemError(args))。
我想测试一些旨在SystemError与文档中详细说明的指定 API 交互的代码,但我不知道如何重新创建SystemError. 这可能吗?
我想使用 C++ 标准库工具 ( ) 读取文件std::ifstream- 当然,如果遇到错误,也可以可靠地报告错误。
显然,这并不是一件容易的事!
std::basic_fstream的(其模板std::ifstream是实例化)默认情况下不会抛出异常。basic_ios::exceptions()(这是 的超超类std::ifstream)。14年前,有人问过这个问题:
答案告诉我们:
errno/GetLastError()会为我们提供非零/非成功值。那真是太糟糕了。另一方面,14年过去了。有什么改变吗?也就是说,对于抛出的异常或errno/GetLastError()被设置是否有更好的保证?如果不是,那么在构建中报告错误的“尽力而为”方法是什么std::fstream?
(我很想问“为什么构造函数在失败时不会抛出异常?”,但我们不讨论这个。)
问题
我编写了一个代码,将一些历史数据作为输入。假设数据集具有timeseries格式,我正在尝试进行回归并找到预测变量。
代码
对于我的项目,我有四个文件:my_project.py、utilities.py、plotter.py和constants.py. 这是两个脚本的一些小部分(相关导入):
my_project.py:
从时间导入 perf_counter
来自常量导入(输出目录、数据路径、输出文件)
从实用程序导入(dataframe_in_nutshell、excel_reader、info_printer、sys、module_creator、process_discovery、data_explanatory_analysis、excel_reader、df_cleaner、feature_extractor、ml_modelling)
从绘图仪导入绘图仪
utilities.py
导入操作系统
导入系统导入检查
从 pathlib 导入路径
输入 import (可迭代、列表、元组、可选)
从 itertools 导入 zip_longest
将 matplotlib.pyplot 导入为 plt
将 statsmodels.tsa.api 导入为 smt
将 statsmodels.api 导入为 sm
将 pandas 导入为 pd
从 sklearn.metrics 导入mean_absolute_error
从 sklearn.preprocessing 导入规模
从 pycaret.regression 导入(设置、compare_models、predict_model、plot_model、finalize_model、load_model)
导入 csv
来自常量导入(np,路径,nan_value,plots_dir,HOURS_PER_WEEK,LAGS_STEP_NUM,rc_params,NA_VALUES,COLUMNS_NAMES,string_columns,LAGS_LABELS,numeric_columns,output_dir,DATAPATH,dtype_dict,train_size)
从 pprint 导入 PrettyPrinter
pp = PrettyPrinter()
将seaborn导入为sns
sns.set()
错误信息
Traceback (most recent call last):
File "c:\Users\username\OneDrive\Desktop\project\my_project.py", …Run Code Online (Sandbox Code Playgroud) 在我的C ++应用程序中,我使用OpenSSL使用非阻塞BIO连接到服务器。我正在为Mac OS X和iOS开发。
第一次调用SSL_shutdown()返回0。这意味着我必须SSL_shutdown()再次调用:
可能会出现以下返回值:
0关闭尚未完成。如果将执行双向关闭,则第二次调用SSL_shutdown()。SSL_get_error的输出可能会产生误导,因为即使没有发生错误,也可能会标记错误的SSL_ERROR_SYSCALL。
<0关闭不成功,因为在协议级别发生了致命错误或发生了连接故障。如果需要采取措施继续非阻塞BIO的操作,也会发生这种情况。使用返回值ret调用SSL_get_error找出原因。
https://www.openssl.org/docs/ssl/SSL_shutdown.html
到目前为止,上帝。问题是在第二次致电时发生SSL_shutdown()。这将返回-1,表示发生了错误(请参见上文)。现在,如果我检查SSL_get_error()得到错误SSL_ERROR_SYSCALL,则该错误又意味着发生了系统错误。但是现在抓住了。如果我检查errno它返回0->未知错误。到目前为止,我对该问题的了解是,这可能意味着该服务器只是“挂断”了,但是说实话,这并不令我满意。
这是我关闭的实现:
int result = 0;
int shutdownResult;
while ((shutdownResult = SSL_shutdown(sslHandle)) != 1) { //close connection 1 means everything is shut down ok
if (shutdownResult == 0) { //we are supposed to call shutdown again
continue;
} else if (SSL_get_error(sslHandle, shutdownResult) == SSL_ERROR_WANT_READ) {
[...] //omitted want read code, in this case the application never reaches this point …Run Code Online (Sandbox Code Playgroud) 我正在派生我自己的异常,调用它MyException,std::system_error从而重写what()以计算并返回我的消息.MyException的初始化列表不会调用带有消息的system_error构造函数覆盖.
如果我抓住一个MyException,并将其复制到std::exception调用的结果what()上std::exception就是nullptr.这是有道理的.
我的问题是,如果我确实使用了在初始化时接收消息的system_exception的构造函数MyException,是否指定system_error将获取消息的副本并拥有它并释放它?
我假设这将使std::exception副本MyException能够返回有效的what().虽然我会在每次MyExceptions创建新内容时计算"什么"需要考虑性能.只有在首次调用what()时,我才能懒惰地计算它.
我有点担心'what'字符串的所有权what()返回a char*而不是a const std::string&.
代码是这样的(我还没有编译):
class MyException : public std::system_error
{
std::string what_;
public:
MyException(int errorValue, const std::error_category& category)
: std::system_error(errorValue, category)
{}
char* what() const
{
what_ = "MyException: " + to_string(code().value());
return what_.c_str();
}
};
int main()
{
std::exception ex;
try
{ …Run Code Online (Sandbox Code Playgroud) system-error ×9
c++ ×6
c++11 ×2
boost ×1
c++14 ×1
dll ×1
errno ×1
exception ×1
fstream ×1
ifstream ×1
javascript ×1
node.js ×1
nonblocking ×1
numba ×1
openssl ×1
pycaret ×1
python-3.x ×1
regression ×1
ssl ×1
std ×1
typescript ×1
unix ×1