<?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[Throw mit string]]></title><description><![CDATA[<p>Ich habe gelesen das man kein stdString werfen sollte, besonders nicht bei std::bad_alloc. Die Alternative ist natürlich ein C-String, aber wie bekomme ich am besten verschiedene Variablen zu einen C-String?</p>
<pre><code>if (status_code != 200)  //mein Versuch ist leider sehr unleserlich
{
    throw std::runtime_error( std::string( std::string(&quot;bad status code &quot;) + std::to_string( status_code ) ).c_str() );
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/329061/throw-mit-string</link><generator>RSS for Node</generator><lastBuildDate>Tue, 07 Jul 2026 06:35:18 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/329061.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 05 Nov 2014 20:56:10 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Throw mit string on Wed, 05 Nov 2014 20:56:10 GMT]]></title><description><![CDATA[<p>Ich habe gelesen das man kein stdString werfen sollte, besonders nicht bei std::bad_alloc. Die Alternative ist natürlich ein C-String, aber wie bekomme ich am besten verschiedene Variablen zu einen C-String?</p>
<pre><code>if (status_code != 200)  //mein Versuch ist leider sehr unleserlich
{
    throw std::runtime_error( std::string( std::string(&quot;bad status code &quot;) + std::to_string( status_code ) ).c_str() );
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2426073</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2426073</guid><dc:creator><![CDATA[DontThrowAStdString]]></dc:creator><pubDate>Wed, 05 Nov 2014 20:56:10 GMT</pubDate></item><item><title><![CDATA[Reply to Throw mit string on Wed, 05 Nov 2014 21:01:45 GMT]]></title><description><![CDATA[<p>DontThrowAStdString schrieb:</p>
<blockquote>
<p>Ich habe gelesen das man kein stdString werfen sollte, besonders nicht bei std::bad_alloc.</p>
</blockquote>
<p>Ignoriere std::bad_alloc. In dem Fall kannst du eh nichts machen. Mach es so, wie du gezeigt hast, aber bedenke, dass Exceptions sehr gerne überbenutzt werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2426074</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2426074</guid><dc:creator><![CDATA[bad_excep]]></dc:creator><pubDate>Wed, 05 Nov 2014 21:01:45 GMT</pubDate></item><item><title><![CDATA[Reply to Throw mit string on Wed, 05 Nov 2014 21:03:03 GMT]]></title><description><![CDATA[<p>Ups, so wie du es gemacht hast, solltest du es besser nicht machen (c_str() ist nicht mehr gültig, sobald das return beendet ist). Mach dir deine eigene Exception-Klasse.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2426075</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2426075</guid><dc:creator><![CDATA[bad_excep]]></dc:creator><pubDate>Wed, 05 Nov 2014 21:03:03 GMT</pubDate></item><item><title><![CDATA[Reply to Throw mit string on Wed, 05 Nov 2014 21:13:01 GMT]]></title><description><![CDATA[<p>bad_excep schrieb:</p>
<blockquote>
<p>Ups, so wie du es gemacht hast, solltest du es besser nicht machen (c_str() ist nicht mehr gültig, sobald das return beendet ist). Mach dir deine eigene Exception-Klasse.</p>
</blockquote>
<p>Danke für die Hilfe! Meinst du damit eine Exception-Klasse die stdString erwartet?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2426078</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2426078</guid><dc:creator><![CDATA[DontThrowAStdString]]></dc:creator><pubDate>Wed, 05 Nov 2014 21:13:01 GMT</pubDate></item><item><title><![CDATA[Reply to Throw mit string on Wed, 05 Nov 2014 22:56:22 GMT]]></title><description><![CDATA[<p>Was genau ist deine Frage? Wofür benötigst du das? Der gezeigte Code von dir ist einwandfrei (obwohl der Cast nach <code>std::string</code> natürlich nicht nötig ist, die Klammern allein reichen).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2426093</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2426093</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Wed, 05 Nov 2014 22:56:22 GMT</pubDate></item><item><title><![CDATA[Reply to Throw mit string on Thu, 06 Nov 2014 05:22:32 GMT]]></title><description><![CDATA[<p>bad_excep schrieb:</p>
<blockquote>
<p>DontThrowAStdString schrieb:</p>
<blockquote>
<p>Ich habe gelesen das man kein stdString werfen sollte, besonders nicht bei std::bad_alloc.</p>
</blockquote>
<p>Ignoriere std::bad_alloc. In dem Fall kannst du eh nichts machen. Mach es so, wie du gezeigt hast, aber bedenke, dass Exceptions sehr gerne überbenutzt werden.</p>
</blockquote>
<p>Ignorieren? Was spricht denn dagegen einen <code>std::runtime_error</code> mit einem Stringliteral als Argument zu werfen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2426108</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2426108</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Thu, 06 Nov 2014 05:22:32 GMT</pubDate></item><item><title><![CDATA[Reply to Throw mit string on Thu, 06 Nov 2014 10:50:26 GMT]]></title><description><![CDATA[<p>Wenn du sowieso schon nen <code>std::string</code> hast, dann verwende auch den <code>std::string const&amp;</code> ctor:</p>
<pre><code class="language-cpp">if (status_code != 200)  //mein Versuch ist leider sehr unleserlich 
{ 
//   throw std::runtime_error( std::string( std::string(&quot;bad status code &quot;) + std::to_string( status_code ) ).c_str() ); 
     throw std::runtime_error(&quot;bad status code &quot; + std::to_string(status_code)); 
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2426143</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2426143</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Thu, 06 Nov 2014 10:50:26 GMT</pubDate></item><item><title><![CDATA[Reply to Throw mit string on Thu, 06 Nov 2014 11:30:25 GMT]]></title><description><![CDATA[<p>Der Grund dafür, dass man bei <code>std::bad_alloc</code> keinen <code>std::string</code> werfen soll ist der, dass <code>std::string</code> selbst noch ein mal Speicher im Freespace für seinen internen Puffer anfordert. Im Falle von <code>std::bad_alloc</code> kann das auch in die Hose gehen, weil Speicher gerade knapp ist.<br />
Ich kann gerade nicht sagen, wie die anderen Exception Klassen der STL das handhaben, also ob hinter <code>what()</code> ein <code>std::string</code> steckt oder nur ein Zeiger.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2426149</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2426149</guid><dc:creator><![CDATA[DocShoe]]></dc:creator><pubDate>Thu, 06 Nov 2014 11:30:25 GMT</pubDate></item></channel></rss>