Home > Backend Development > C++ > How do lambda expressions handle exceptions in C++?

How do lambda expressions handle exceptions in C++?

WBOY
Release: 2024-04-17 12:42:02
Original
1030 people have browsed it

In C, there are two ways to handle exceptions using Lambda expressions: catch the exception using a try-catch block, and handle or rethrow the exception in the catch block. Use a wrapper function of type std::function whose try_emplace method can catch exceptions in Lambda expressions.

lambda 表达式在 C++ 中如何处理异常?

Using Lambda expressions to handle exceptions in C

Introduction

Lambda An expression is an anonymous function that captures external variables and passes parameters by value or reference. In C, lambda expressions can be used for a variety of purposes, including handling exceptions.

Using try-catch blocks

The try-catch block is the standard way to handle exceptions in Lambda expressions. The catch block allows catching specific types of exceptions or all exceptions. The following example demonstrates how to use a try-catch block in a Lambda expression to handle exceptions:

#include <functional>
#include <iostream>

int main() {
  auto lambda =  [](int x) -> int {
    try {
      return x / 0;  // 将引发 std::runtime_error 异常
    } catch (const std::exception& e) {
      std::cout << "Exception caught: " << e.what() << std::endl;
      return -1;
    }
  };

  int result = lambda(10);
  std::cout << "Result: " << result << std::endl;
  return 0;
}
Copy after login

Using std::function

Another way to handle exceptions in a Lambda expression The exception method is to use std::function. std::function is a wrapper function that can accept different function types, including Lambda expressions. std::function provides a try_emplace method that allows exceptions to be caught in Lambda expressions. The following example demonstrates how to use std::function to handle exceptions:

#include <functional>
#include <iostream>

int main() {
  std::function<int(int)> lambda;
  try {
    lambda = [](int x) -> int { return x / 0; };  // 将引发 std::runtime_error 异常
  } catch (const std::exception& e) {
    std::cout << "Exception caught: " << e.what() << std::endl;
    lambda = [](int x) -> int { return -1; };
  }

  int result = lambda(10);
  std::cout << "Result: " << result << std::endl;
  return 0;
}
Copy after login

Practical example

Consider a function with the following interface:

int do_something(const std::string& input);
Copy after login

This function may raise a std::invalid_argument exception if the input is invalid. We can use a Lambda expression and a try-catch block to handle this exception as follows:

auto do_something_safe = [](const std::string& input) -> int {
  try {
    return do_something(input);
  } catch (const std::invalid_argument& e) {
    // 处理异常并返回 -1
    std::cout << "Invalid input: " << e.what() << std::endl;
    return -1;
  }
};
Copy after login

We can then safely call do_something_safe in our code , without explicitly handling the exception.

The above is the detailed content of How do lambda expressions handle exceptions in C++?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template