C++ syntax: Difference between revisions

Content deleted Content added
Tag: Reverted
Line 482:
<syntaxhighlight lang="cpp" line="1">
import std;
 
using Exception = std::exception;
using OutOfRangeException = std::out_of_range;
using Vector = std::vector;
 
int main() {
try {
std::vectorVector<int> vec{3, 4, 3, 1};
int i{vec.at(4)}; // Throws an exception, std::out_of_range (indexing for vec is from 0-3 not 1-4)
} catch (const std::out_of_rangeOutOfRangeException& e) {
// An exception handler, catches std::out_of_range, which is thrown by vec.at(4)
std::println(stderr, "Accessing a non-existent element: {}", e.what());
} catch (const std::exceptionException& e) {
// To catch any other standard library exceptions (they derive from std::exception)
std::println(stderr, "Exception thrown: {}", e.what());