Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
BadCastAllExcept
//g++ 7.4.0 ////////////////////////////////////////////////////////////////////////////// //BadCastAllExcept: understanding more C++ exception and errors //this code is created by Rezaul Hoque on June 21,2022; //contact:jewelmrh@yahoo.com;Dhaka,Bangladesh;https://rezaulhoque.wordpress.com,https://hoquestake.blogspot.com //note: codes shared by Rezaul Hoque on rextester are not for sale; they are created and shared to facilitate the algorithm learning process; many like Hoque use this platform to practice programming ;Rezaul hopes his contribution helps others to fine tune their learning; //////////////////////////////////////////////////////////////////////////// #include <iostream> #include <typeinfo> #include <exception> #include <stdexcept> void unexpected () { throw; } void test() throw(std::bad_exception) { throw std::runtime_error("exception test"); } class A{ char c; public: A(){} ~A(){} void func(); }; /*** class B{ int g; public: B(){} ~B(){} }; ***/ int main() { //std::bad_cast: failure to runtime check of dynamic cast because types don't have inherent link [source: https://en.cppreference.com] /*** B f; try{ A& a=dynamic_cast<A&>(f); }catch(std::bad_cast& e) { std::cout<<e.what(); } ***/ //std::bad_alloc: failure to allocate storage[source: https://en.cppreference.com] /*** try{ while (true){ int a=999999999; int* c= new int[a]; } }catch (std::bad_alloc& e) { std::cout<<e.what(); } ***/ //bad_typeid: /*** A* s=NULL; try{ std::cout<<typeid(*s).name(); }catch(const std::bad_typeid& e) { std::cout<<e.what(); } ***/ //std::bad_exception: dynamic expected specification is violated and std::unexpected throws an exception that violates exception specification [source: https://en.cppreference.com] std::set_unexpected (unexpected); try{ test(); }catch (const std::bad_exception & e) { std::cout<<e.what(); } return 0; }
run
|
edit
|
history
|
help
0
BST to DLL
project: bank account
VecHotel3
nearest
For Hello World
Kth smallest element
NamespaceOverload
hack qus
CIDP2k19
error