Updated March 31, 2023
Definition of C++ noexcept Method
In every programming language, we need to have a mechanism to handle the exception, so in C++ we use noexcept to handle error handling. We can surround our methods, lambda, and methods with noexcept keyword in C++ which simply means that this part of code does not throw any exception. In short, we use noexcept keyword with those lines of code that does not cause an exception, and if they cause we can ignore it. This noexcept takes a boolean value as true or false in order to specify whether the function is supposed to throw an exception or not. We have several advantages of using noexcept in our program like it optimize the code, also they are safe to use with the other piece of code. We will discuss this in a later coming section.
Syntax:
We can use the noexcept method with our functions, lambda expressions, and methods to specify that this piece of code can be considered safe and does not cause any exception at runtime. We will discuss its syntax how we can use this to implement it in our program. We will see one practice syntax for better understanding see below;
// it may a throw exception
void mydemofun1() noexcept(false);
// it will not throw exception
void mydemofun1() noexcept(true);
// it will not throw exception
void mydemofun1() noexcept;
// it will not throw exception
void mydemofun1() throw();
As in the above lines of code we have three different synatx to define the noexcept method with our functions in C++, we can use Boolean values to specify their behavior. We will see a practice example in the coming section of the tutorial.
How noexcept Method works in C++?
As of now we know that in c++ we use noexcept method to handle exceptions. That means we can write our code in a function and use noexcept method to decide whether this function will throw an exception or not. This will simply tell this piece of code is safe and does not throw any exception and we can use this with another piece of code that does not throw any exception either. Also, noexcept method takes one argument which is Boolean and here we can also specify the value of this function true or false depending on the requirement. In this section, we will see one example with the reason why we would require to use this and also what are the advantages of using noexcept method in our program.
By using noexcept method we can guarantee that this function is of going to throw an exception which will allow some of the compiler optimizations and increase the performance. Also one more thing this will not cause our program to terminate if the exception occurs that we do not want. As we go into more detail of noexcepm method it has been introduced in C++ 11 very late.
If we talk about the noexcept operator is simply responsible to perform or provide the compile-time check on the function. It returns Boolean true or false depending on the computation. If the operator returns true that means it does not throw any exception, else it will throw. When we use noexcept method it represents a non-throwing function in c++ which is safe while using inside a non-throwing method. Now we will see how we can define our function as a non-throwing exception function in c++ see below;
e.g.:
void sum() noexcept;
As above we are defining our function by using noexcept method here this represents that this function is safe and does not throw an exception if it does we can ignore it. But here we can pass a parameter inside this noexcept method which is a Boolean here. We have two cases for it which are as follows;
1) true: If we specify the value of the noexcept function as true that means this is equivalent to the noexcept method. So it will not throw any exception. Let’s see its syntax for better understanding see below;
e.g.
void sum() noexcept (true);
2) false : If we specify the value of the noexcept function as false that means this is not equivalent to the noexcept method. So it will throw an exception. Let’s see its syntax for better understanding see below;
e.g.
void sum() noexcept (false);
So we can use our noexcept methods in these three different ways in our programming. In c++ we have some functions which are non-throwing in nature by default let’s see the name of each;
1) copy constructor
2) default constructor
3) destructors
4) move constructor
5) copy operator
6) move operator
Points to remember while working with noexcept method;
- It is used to define a non-throwing function in C++.
- It can take Boolean as true or false.
Example
1) In this example we are creating multiple functions which are using noexcept method to deal with the exception. We have also created one function which is not used noexcept method and throws the exception. This is for better understating of the function in c++.
Example #1
Code:
#include <iostream>
#include <stdexcept>
void demofun2() {
throw std::runtime_error("Here we are throwing runtime error. !!");
}
void demofun1() {
throw std::runtime_error("Here we are throwing runtime error. !!");
}
void demofun3() {
std::cout << "I am a safe piece of code :) :) :)!!!!" << "\n";
}
int main() {
std::cout << "Here calling first function with noexcept" << "\n";
std::cout << noexcept(demofun1()) << '\n';
std::cout << "Here calling third function with noexcept" << "\n";
std::cout << noexcept(demofun3()) << '\n';
std::cout << noexcept(100+ 100) << '\n';
std::cout << "Here calling second function without noexcept so it will cause exception for us !!" << "\n";
demofun2();
}
Output:
Conclusion
We use noexcept method to deal with the exception. This is used to define the non-throwing function in c++. By using noexcept we can guarantee our code that it is an exception free and we can use this inside the non-throwing method as well. It will allow compiler optimization and improve performance as well.
Recommended Articles
This is a guide to C++ noexcept. Here we discuss the Definition and How noexcept Method works in C++? with examples respectively. You may also have a look at the following articles to learn more –