Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

assert vs try catch

Status
Not open for further replies.

Haunter

Programmer
Jan 2, 2001
379
US
I am wondering which is the better methond of implemetation?

METHOD # 1
Code:
                try
                {            
                    /*  try catch block may not BE
                        best error implementation */
                    m_p = new char[m_size];
                }    
                catch(bad_alloc xa)
                {
                    cout << "Allocation Errors\n";
                    exit(1);
                }
or
METHOD #2
Code:
                m_p = new char[m_size];
                assert(m_p != 0); /*Another error preventtion tool for above can be turned off with macors */


haunter@battlestrata.com
 
try, throw, catch is better for building APIs that other people will be using in client programs. It allows the developer of the program decide what to do when an error occurs -- as opposed to just crashing.

abort and assert are useful for debuging purposes and development, but I wouldn't leave asserts in a final product.

In general it depends on the application and a ton of other variables.
 
Assert approach: cry (with cryptomessage) then die...

try/catch approach: have a warning then have a chance to recover or make a memorial with clear script...

Make your choice. As jstreich said, it depends on...
 
assert = bug detector

exception = handling and "exceptional" situation

See faq207-5048

/Per

&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top