Skip to content

Latest commit

 

History

History
41 lines (31 loc) · 953 Bytes

CppAssertExample2.md

File metadata and controls

41 lines (31 loc) · 953 Bytes

assert example 2: basics with informative output is an assert example.

From within Qt Creator, this example shows how to view the assert output in the console window.

#include <cassert>
#include <csignal>
#include <cstdlib>
#include <iostream>

void on_abort(int)
{
  std::exit(1);
}

int main()
{
  //Connect the abort signal to the on_abort, to obtain informative
  //screen output with 'Projects | Run | Run in terminal' unchecked
  std::signal(SIGABRT,on_abort);

  const double numerator = 1.0;
  const double denominator = 0.0;
  assert(denominator != 0.0);
  const double result = numerator / denominator;
  std::cout << result << '\n';
}

Console output will be:

Assertion failed!

Program: my_path/my_program
File: my_path/main.cpp, Line 19

Expression: denominator != 0.0