简体   繁体   English

异常处理

[英]exception handling

I get the following core dump msg: 我得到以下核心转储消息:

terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr Abort - core dumped 抛出'std :: out_of_range'实例之后的终止调用what():basic_string :: substr中止-核心转储

I am reading a 14 digit hexadecimal number from a large file. 我正在从一个大文件中读取一个14位十六进制数字。 periodically i notice that there are these blank lines (well i am not sure what it is. How do I handle this exception? maybe a try catch thingy? It looks something like below: 定期地,我注意到有这些空行(嗯,我不确定这是什么。我该如何处理该异常?也许是尝试捕获的东西?它看起来像下面这样:

123456789ABCDE
123456789ABCDE
123456789ABCDE

123456789ABCDE

I am not sure what hidden symbol is occupying the space but its causing issues and i am not sure how to handle this..any ideas? 我不确定哪个隐藏符号正在占用空间,但是会导致问题,并且我不确定如何处理此问题。 maybe i could get a sample of how to handle it? 也许我可以获得如何处理的样本?

You need to post more code but from the exception it looks like you're reading the file line by line and storing the line in an std::string , then calling std::string::substr() to extract 14 characters you want. 您需要发布更多代码,但从例外来看,您似乎是逐行读取文件并将该行存储在std::string ,然后调用std::string::substr()提取所需的14个字符。

Assuming your code looks like this: 假设您的代码如下所示:

std::string str;         /* the lines are stored in this string */
std::string substring;   /* extracted substring stored here */

/* Read file line by line */
// ...

substring = str.substr( index, 14 );  //extract 14 characters starting at 'index'

Change this to: 更改为:

if( str.size() > index ) {
  substring = str.substr( index, 14 );
}

You probably need to include input validation before you try the conversion. 在尝试进行转换之前,您可能需要包括输入验证。 Never accept external input as is and always validate. 切勿按原样接受外部输入,并始终进行验证。

The Robustness principle (also known as Postel's law): 稳健性原则 (也称为Postel定律):

Be conservative in what you send; 保守发送的内容; be liberal in what you accept. 在接受的内容上保持自由。

So, one possibility is just to ignore/skip the malformed lines. 因此,一种可能性就是忽略/跳过格式错误的行。

Others have given the specific answer for this specifc issue. 其他人给出了针对此特定问题的具体答案。

But in general run this in a debugger and see where it throws. 但通常在调试器中运行此命令,然后查看其抛出的位置。 For gdb do 'catch throw' gdb will then break at the point where the error is. 对于gdb,请执行“ catch throw”,然后gdb将在错误所在的位置中断。 It will be pretty obvious what the cause is. 原因很明显。

The following code likely shows the possible issue: 以下代码可能显示了可能的问题:

#include <iostream>

int main(){
    std::string s = "Hi";

    std::string sb = s.substr(14, 0);  // extract 0 characters from 
                                       // an out-of-range 
                                       // start location
}

basic_string<charT,traits,Allocator>
substr(size_type pos = 0, size_type n = npos) const;

If the start index(first argument) is invalid (greater than the size of the string object), the standard mandates the method to throw 'out_of_range' error. 如果起始索引(第一个参数)无效(大于字符串对象的大小),则标准要求该方法抛出“ out_of_range”错误。

It also looks like you are not having the string operations with try/catch. 看起来您没有使用try / catch进行字符串操作。 This is not a good practise. 这不是一个好习惯。 Have all code that can throw exceptions within a try catch block. 在try catch块中包含所有可能引发异常的代码。 This gives you a chance to handle the exception (if you so wish). 这使您有机会处理异常(如果您愿意)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM