Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> using namespace std; int main() { try { throw std::logic_error( "Hallo Welt!" ); } catch ( const std::logic_error& e ) { std::cout << e.what() << std::endl; } return 0; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> using namespace std; int main() { std::cout<<"Hallo Welt!"<<endl; return 0; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> #include <stdexcept> float divide( int a, int b ) throw( std::logic_error ) { if ( b == 0 ) throw std::logic_error( "Divide by null" ); return a / b; } int main() { int a = 10; int b = 0; //std::cout << ( a / b ) << std::endl; // undefinierbares verhalten try { divide( a, b ); } catch ( const std::logic_error& e ) { std::cout << "Error: " << e.what() << std::endl; } } |
Zitat
Und wenn du eine Ausnahme wirfst so kannst du Informationen in ein Objekt packen und dieses als "Ausnahmeobjekt" werfen. Sodass du anderswo (im catch-Block) auf die Informationen zugreifen kannst und ggf weißt was für ein Fehler wo warum aufgetreten ist...
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#include <iostream> #include <stdexcept> using namespace std; float divide( float a, float b ) throw( std::logic_error ) { if ( b == 0 ) throw std::logic_error( "Divide by null" ); return a / b; } int main() { float a ; float b ; std::cin>>a; std::cout<<endl; std::cin>>b; //std::cout << ( a / b ) << std::endl; // undefinierbares verhalten try { divide( a, b ); } catch ( const std::logic_error& e ) { std::cout << "Error: " << e.what() << std::endl; } std::cout<<a/b<<endl; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 |
try { float e = divide( a, b ); std::cout<<e<<endl; } catch ( const std::logic_error& e ) { std::cout << "Error: " << e.what() << std::endl; } |
Werbeanzeige