Skip to content

Instantly share code, notes, and snippets.

@androm3da
Created June 2, 2026 13:49
Show Gist options
  • Select an option

  • Save androm3da/cd8b67e2f7ca22e319193f012640e96f to your computer and use it in GitHub Desktop.

Select an option

Save androm3da/cd8b67e2f7ca22e319193f012640e96f to your computer and use it in GitHub Desktop.
test C++ exceptions
#!/bin/bash
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
# SPDX-License-Identifier: BSD-3-Clause-Clear
#
# Build and run a C++ exception handling test for Hexagon Linux using
# clang+llvm-22.1.4-cross-hexagon-unknown-linux-musl and qemu-hexagon.
set -euo pipefail
TOOLCHAIN=/local/mnt/workspace/install/clang+llvm-22.1.4-cross-hexagon-unknown-linux-musl/x86_64-linux-gnu
CXX=$TOOLCHAIN/bin/hexagon-linux-musl-clang++
SYSROOT=$TOOLCHAIN/target/hexagon-linux-musl
QEMU=$TOOLCHAIN/bin/qemu-hexagon
WORKDIR=$(mktemp -d)
trap 'rm -rf "$WORKDIR"' EXIT
SRC=$WORKDIR/test_exceptions.cpp
BIN=$WORKDIR/test_exceptions
# ---------------------------------------------------------------------------
# Emit the test source
# ---------------------------------------------------------------------------
cat > "$SRC" << 'EOF'
// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
// SPDX-License-Identifier: BSD-3-Clause-Clear
#include <iostream>
#include <stdexcept>
#include <string>
// Custom exception class
class HexagonTestError : public std::runtime_error {
public:
int code;
explicit HexagonTestError(const std::string &msg, int code)
: std::runtime_error(msg), code(code) {}
};
// Function that throws a standard exception
void throw_runtime_error() {
throw std::runtime_error("runtime_error from throw_runtime_error()");
}
// Function that throws a custom exception
void throw_custom_error(int code) {
throw HexagonTestError("custom error with code " + std::to_string(code), code);
}
// Function exercising nested try/catch and re-throw
void nested_rethrow() {
try {
throw std::logic_error("inner logic_error");
} catch (const std::logic_error &e) {
std::cout << " [inner catch] caught: " << e.what() << " -- rethrowing\n";
throw; // rethrow
}
}
int main() {
int pass = 0, fail = 0;
// Test 1: catch std::runtime_error by reference
std::cout << "Test 1: catch std::runtime_error\n";
try {
throw_runtime_error();
std::cout << " FAIL: no exception thrown\n";
++fail;
} catch (const std::runtime_error &e) {
std::cout << " PASS: caught \"" << e.what() << "\"\n";
++pass;
}
// Test 2: catch custom exception, check polymorphism via base class
std::cout << "Test 2: catch HexagonTestError via std::runtime_error base\n";
try {
throw_custom_error(42);
std::cout << " FAIL: no exception thrown\n";
++fail;
} catch (const HexagonTestError &e) {
std::cout << " PASS: caught HexagonTestError code=" << e.code
<< " msg=\"" << e.what() << "\"\n";
++pass;
}
// Test 3: catch base class when derived is thrown
std::cout << "Test 3: catch base std::exception for HexagonTestError\n";
try {
throw_custom_error(7);
std::cout << " FAIL: no exception thrown\n";
++fail;
} catch (const std::exception &e) {
std::cout << " PASS: caught via std::exception: \"" << e.what() << "\"\n";
++pass;
}
// Test 4: nested try/catch with rethrow
std::cout << "Test 4: nested rethrow\n";
try {
nested_rethrow();
std::cout << " FAIL: no exception thrown\n";
++fail;
} catch (const std::logic_error &e) {
std::cout << " PASS: outer catch got rethrown: \"" << e.what() << "\"\n";
++pass;
}
// Test 5: catch(...) fallback
std::cout << "Test 5: catch(...) fallback\n";
try {
throw 99; // plain int
std::cout << " FAIL: no exception thrown\n";
++fail;
} catch (int v) {
std::cout << " PASS: caught int " << v << "\n";
++pass;
} catch (...) {
std::cout << " FAIL: fell through to catch(...) instead of catch(int)\n";
++fail;
}
// Test 6: no exception -- ensure normal flow not disrupted
std::cout << "Test 6: no exception (normal flow)\n";
try {
int x = 1 + 1;
(void)x;
++pass;
std::cout << " PASS: no exception, normal flow ok\n";
} catch (...) {
std::cout << " FAIL: unexpected exception\n";
++fail;
}
std::cout << "\nResults: " << pass << " passed, " << fail << " failed\n";
return fail == 0 ? 0 : 1;
}
EOF
# ---------------------------------------------------------------------------
# Build
# ---------------------------------------------------------------------------
echo "Building $SRC ..."
"$CXX" -std=c++17 -O1 -fexceptions -o "$BIN" "$SRC"
echo "Built $BIN"
echo
# ---------------------------------------------------------------------------
# Run
# ---------------------------------------------------------------------------
echo "Running on qemu-hexagon ..."
"$QEMU" -L "$SYSROOT" "$BIN"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment