473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

catching assertion error

is it possible to catch an assertion error? that is, could the following
block of code be made to run?

assert (false);
catch (...) {}
Nov 7 '06 #1
6 30631
Eric wrote:
is it possible to catch an assertion error? that is, could the following
block of code be made to run?

assert (false);
catch (...) {}
No. Asserts are different from exceptions, and if you want to catch,
you must throw.

Cheers! --M

Nov 7 '06 #2
Eric wrote:
is it possible to catch an assertion error? that is, could the following
block of code be made to run?
assert (false);
catch (...) {}
The standard assert just prints some information and calls abort. Of course
you can write your own assert macro and not include the standard header,
but is clearer to use another name, such as ASSERT, MY_PROJECT_ASSERT... or
better yet, don't use in the name nothing similar to assert if you want to
use it as an exception and not as an assertion. For example, something like
LOGIC_CHECK that throws std::logic_error if failed looks reasonable to me.

--
Salu2
Nov 7 '06 #3

Eric wrote:
is it possible to catch an assertion error? that is, could the following
block of code be made to run?

assert (false);
catch (...) {}
No, if you want to catch, someone has to throw the ball (or whatever).

#include <iostream>
#include <stdexcept>

int main()
{
try
{
throw std::logic_error( "testing logic_error" );
}
catch ( const std::exception& r_e )
{
std::cerr << "error: " << r_e.what() << std::endl;
}
}

/*
error: testing logic_error
*/

Can you see why its a bad idea to catch(...) as opposed to a specific
catch?

Nov 7 '06 #4
VJ
Salt_Peter wrote:
>
Can you see why its a bad idea to catch(...) as opposed to a specific
catch?
Why is it a bad idea to catch all?
It has some good uses
Nov 8 '06 #5
VJ wrote:
Salt_Peter wrote:

Can you see why its a bad idea to catch(...) as opposed to a specific
catch?

Why is it a bad idea to catch all?
It has some good uses
He's not suggesting it's *always* a bad idea to catch all exceptions,
but in the example he gave, it is better to catch a specific exception
(or rather, a specific class of exceptions).

Cheers! --M

Nov 8 '06 #6

VJ wrote:
Salt_Peter wrote:

Can you see why its a bad idea to catch(...) as opposed to a specific
catch?

Why is it a bad idea to catch all?
It has some good uses
Consider the list of possible standard exceptions that might be thrown:
ios_base::failure
bad_alloc
bad_cast
logic_error
runtime_error
bad_exception

It would be a shame to catch any of these in a carch-all for obvious
reasons.

#include <iostream>
#include <stdexcept>

int main()
{
try {
// may throw
}
catch ( const std::logic_error & e ) {
std::cerr << "a logic_error was caught: ";
std::cerr << e.what() << std::endl;
}
catch ( const std::exception & e ) {
std::cerr << "a specific error was caught: ";
std::cerr << e.what() << std::endl;
}
catch ( ... ) {
std::cerr << "an undetermined error occurred\n";
}
}

Nov 8 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Todd Miller | last post by:
Hi, I recently discovered an assertion failure in the Python garbage collection system when scripts using our C extension (numarray) exit. The assertion is activated for Pythons configured using...
0
by: benevilent | last post by:
Hey, I'm getting an assertion error as a result of embedding python. "Modules/gcmodule.c:231: visit_decref: Assertion `gc->gc.gc_refs != 0' failed." I only get this assertion error with...
20
by: BigMan | last post by:
On most architectures integer division by 0 results in throwing an exception (does anyone know of one that behaves differently?). I need a way to catch FP division by 0. Is there a standard way to...
7
by: cmay | last post by:
FxCop complains every time I catch System.Exception. I don't see the value in trying to catch every possible exception type (or even figuring out what exceptions can be caught) by a given block...
1
by: Timur Safin | last post by:
Hi All, Sorry if it is offtopic here, I wasn't able to find any more relevant group... I'm slowly approaching AMD64 build for our product (as my personal fun project). And after I ran that...
5
by: Ron Louzon | last post by:
I have some C++ code that uses the CSingleLock( CCriticalSection *) constructor. In visual C++ 6.0, this code compiles and runs fine in both Debug and release modes. However, in Visual Studio...
2
by: Penny Balkwill via .NET 247 | last post by:
(Type your message here) I am supporting a system which uses ORACLE Forms, and we are getting an intermittent error: Assertion failed! Program: D:\Dev6i\bin\ifrun60.exe File:...
7
by: inkexit | last post by:
I'm getting assertion errors when I try to import or export a file using this code. Please help. I don't even know what an assertion error is. #include <iostream> #include <iomanip> #include...
4
by: Mullai | last post by:
Hi , My program gives an error message like this Debug Assertion Failed! program:................ File: wincore.cpp Line: 958 Please can anyone help me out in this issue. I have to solve...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.