<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Parsen von strings in double mit error handling]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe folgende Funktion:</p>
<pre><code class="language-cpp">std::vector&lt; double &gt; ParseDoubleLine( const str_type &amp; content )
{
    const auto substr = tokenize( content );

    std::vector&lt; double &gt; test_vec;
    test_vec.reserve( substr.size() );

    for( const auto &amp; i : substr )
    {
        test_vec.push_back( std::stod( i ) );
    }

    return test_vec;
}
</code></pre>
<p>Jetzt möchte ich ein sauberes error handling.<br />
Die Funktion stod wirft laut Spezifikation invalid_argument und out_of_range.<br />
Wenn ich die Fehlerbehandlung innerhalb der Funktion habe, dann brauche ich etwas zur Fehleranzeige.<br />
Ein weiteres Argument sieht doof aus. Auch möchte ich nicht den return wert ändern weil ich dann so etwas nicht kann:</p>
<pre><code class="language-cpp">void andereFnc()
{
    // sourcecode ...
    const auto test_vec = ParseDoubleLine( content );
}
</code></pre>
<p>Um einen kurzen try/catch block zu haben ist sowas denkbar:</p>
<pre><code class="language-cpp">std::vector&lt; double &gt; test_vec;

    try
    {
        test_vec = ParseDoubleLine( content );
    }
    catch( std::invalid_argument &amp; e )
    {
        // ...
    }
    catch( std::out_of_range &amp; e )
    {
        // ...
    }
}
</code></pre>
<p>Damit verliere ich aber das schöne const auto.<br />
Ich möchte auch nicht den try catch Block ins Unendliche ziehen, weil der Quellcode dann wieder unübersichtlicher wird.</p>
<p>Wie löst ihr solche Dinge? Oder mache ich generell was falsch?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/340479/parsen-von-strings-in-double-mit-error-handling</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Apr 2026 02:52:04 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/340479.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 10 Nov 2016 15:58:10 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Parsen von strings in double mit error handling on Thu, 10 Nov 2016 15:58:10 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe folgende Funktion:</p>
<pre><code class="language-cpp">std::vector&lt; double &gt; ParseDoubleLine( const str_type &amp; content )
{
    const auto substr = tokenize( content );

    std::vector&lt; double &gt; test_vec;
    test_vec.reserve( substr.size() );

    for( const auto &amp; i : substr )
    {
        test_vec.push_back( std::stod( i ) );
    }

    return test_vec;
}
</code></pre>
<p>Jetzt möchte ich ein sauberes error handling.<br />
Die Funktion stod wirft laut Spezifikation invalid_argument und out_of_range.<br />
Wenn ich die Fehlerbehandlung innerhalb der Funktion habe, dann brauche ich etwas zur Fehleranzeige.<br />
Ein weiteres Argument sieht doof aus. Auch möchte ich nicht den return wert ändern weil ich dann so etwas nicht kann:</p>
<pre><code class="language-cpp">void andereFnc()
{
    // sourcecode ...
    const auto test_vec = ParseDoubleLine( content );
}
</code></pre>
<p>Um einen kurzen try/catch block zu haben ist sowas denkbar:</p>
<pre><code class="language-cpp">std::vector&lt; double &gt; test_vec;

    try
    {
        test_vec = ParseDoubleLine( content );
    }
    catch( std::invalid_argument &amp; e )
    {
        // ...
    }
    catch( std::out_of_range &amp; e )
    {
        // ...
    }
}
</code></pre>
<p>Damit verliere ich aber das schöne const auto.<br />
Ich möchte auch nicht den try catch Block ins Unendliche ziehen, weil der Quellcode dann wieder unübersichtlicher wird.</p>
<p>Wie löst ihr solche Dinge? Oder mache ich generell was falsch?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2514881</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2514881</guid><dc:creator><![CDATA[asdasddsasd]]></dc:creator><pubDate>Thu, 10 Nov 2016 15:58:10 GMT</pubDate></item><item><title><![CDATA[Reply to Parsen von strings in double mit error handling on Thu, 10 Nov 2016 16:35:26 GMT]]></title><description><![CDATA[<p>lass die exception von derjenigen person behandeln, die für den <code>content</code> zuständig ist und verzichte auf den <code>try/catch</code> block. wenn du vor ort &quot;nur&quot; ein <code>what</code> zu debugging-zwecken ausgeben willst, dann mach den <code>try</code> -block einfach größer und wirf weiter.</p>
<pre><code class="language-cpp">void foo (const string&amp; content) 
try {
    const auto value = stod(content);  
} 
catch (const std::logic_error&amp; ex) {
    cerr &lt;&lt; &quot;error: &quot; &lt;&lt; ex.what() &lt;&lt; '\n';
    throw;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2514887</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2514887</guid><dc:creator><![CDATA[dove]]></dc:creator><pubDate>Thu, 10 Nov 2016 16:35:26 GMT</pubDate></item></channel></rss>