tags:

views:

70

answers:

5
+1  Q: 

exception handling

Hi guys, 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

I am reading a 14 digit hexadecimal number from a large file. 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?

+1  A: 

You probably need to include input validation before you try the conversion. Never accept external input as is and always validate.

David
+1 for a solid comment. I think the key point here is that ideally you'd do a little general validation of the input lines - if performance isn't critical a one-liner regexp would serve - rather than just hoping that something you do in input-optimistic processing code happens to throw an exception for you (the poster asked about this option). It's too easy for the result simply to be wrong at a business-logic level without the individual data-handling operations being wrong at this kind of indexing and dumb-data handling level.
Tony
+1  A: 

The Robustness principle (also known as Postel's law):

Be conservative in what you send; be liberal in what you accept.

So, one possibility is just to ignore/skip the malformed lines.

ArunSaha
Moving from "be liberal in what you accept" to "ignore/skip the malformed lines" is a dubious recipe for robustness. You want to be liberal in _considering_ possible input, so you can validate that you got legitimate input. So, accept in the sense of handling it, but don't hesitate to reject it at a business logic level - which in this case properly forces checking on how that file was generated. Otherwise you may well be masking a bug upstream that will come back to haunt you or undermine your results anyway. What if a value's missing, rather than the extra newline being spurious?
Tony
+3  A: 

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.

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 );
}
Praetorian
`substr` quietly clips the returned range if it extends past the end. A check only for `index <= str.size()` suffices. +1, though.
Potatoswatter
@Potatoswatter, good point, thanks, I'll update the answer
Praetorian
+1  A: 

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. It will be pretty obvious what the cause is.

pm100
A: 

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.

It also looks like you are not having the string operations with try/catch. This is not a good practise. Have all code that can throw exceptions within a try catch block. This gives you a chance to handle the exception (if you so wish).

Chubsdad