This question already has answers here:
Does throw inside a catch ellipsis (...) rethrow the original error in C++?
(1 answer)
C++ re-throw an exception caught by
(2 answers)
In C++, is there a difference between “throw” and “throw ex”?
(8 answers)
Closed 1 year ago.
Our teacher gave us a list of code snippets to prepare for our final exam. For each one, we have to tell either the code compiles, and if so to guess what it outputs. Here is one of them:
#include <iostream>
using namespace std;
float f(int y){
try{
if(y % 2)
throw y / 2;
}
catch(int i){
if (i % 2) throw;
cout << "Number " << i << "is not good! " << "\n";
}
return y / 2;
}
int main(){
int x;
try{
cout << "Give me an integer: ";
cin >> x;
if(x) f(x);
cout << "Number " << x << " not good!\n";
}
catch(int i){
cout << "Number " << i << " is good ! " << "\n";
}
return 0;
}
What does that throw (no argument) really do? Does it just re-throw the current exception, meaing "i"? Is there more I should now or understant from this example ?
Related
This question already has answers here:
Why doesn't 'd /= d' throw a division by zero exception when d == 0?
(5 answers)
Closed 1 year ago.
I am learning about try-catch constructs in C++ and I have the following example that appears to fail to execute the code inside either of the catches. I have spent the past few hours trying to find the bug/issue without luck.
I am wondering if there is an issue with g++ on my machine -- I am using mingw's g++ and Windows 10.
#include <iostream>
#include <stdexcept>
int main(){
try {
std::cout << "Start of Try-Catch\n";
int a = 13;
int b = 0;
int p = a/b;
std::cout << "printing p: " << p << std::endl;
p = 43;
std::cout << "Passed the div by zero issue\n";
} catch (std::runtime_error& e){
std::cout << "runtime error: " << e.what() << '\n';
return 2;
} catch (std::exception& e){
std::cout << "other error: " << e.what() << '\n';
return 3;
} catch (...) {
std::cout << "final catch\n";
return 4;
}
std::cout << "end of program\n";
return 0;
}
Instead, this is what happens when I compile and run:
C:\Users\...\Part 1>g++ cp_bug.cpp -std=c++17
C:\Users\...\Part 1>a.exe
Start of Try-Catch
C:\Users\...\Part 1>
it would be more logical to do something like that:
int main(){
try {
std::cout << "Start of Try-Catch\n";
int a = 13;
int b = 0;
if(b==0)
throw std::string("Passed the div by zero issue\n");
int p = a/b;
std::cout << "printing p: " << p << std::endl;
} catch (std::string e) {
std::cout << e;
return -1;
}
std::cout << "end of program\n";
return 0;
}
Your problem is that division by zero doesn't throw an exception that can be handled. Try the following tutorial instead.
Also this question is duplicated.
This question already has answers here:
Order of execution in operator <<
(4 answers)
Closed 6 years ago.
Consider the following code. expected output should be
0 1
1 2
2 3
and so on.
#include<iostream>
using namespace std;
int f=0;
int B()
{
return f;
}
int A()
{
return f++;
}
int main()
{
cout<<A()<<" "<<B()<<endl;
cout<<A()<<" "<<B()<<endl;
cout<<A()<<" "<<B()<<endl;
cout<<A()<<" "<<B()<<endl;
return 0;
}
but the actual output is
0 0
1 1
2 2
and so on.. why?
and if i change code like this-
int main()
{
int f=0;
cout<<f++<<" "<<f<<endl;
cout<<f++<<" "<<f<<endl;
cout<<f++<<" "<<f<<endl;
cout<<f++<<" "<<f<<endl;
return 0;
}
then i get correct expected output
why?
The order of evaluation of the operands of << is not specified. So
cout << A() << " " << B() << endl;
can be treated as either:
temp1 = A();
temp2 = B();
cout << temp1 << " " << temp2 << endl;
or as:
temp2 = B();
temp1 = A();
cout << temp1 << " " << temp2 << endl;
Performing side effects on a variable and accessing it without defined sequencing results in undefined behavior.
This question already has answers here:
possible assignment in condition (C)
(2 answers)
Closed 8 years ago.
I'm new to StackExchange, but I have a simple class that seems to not return to the correct result when I run it.
Here is the code:
#include <iostream>
using namespace std;
int thisIsHowYouIfLikeABoss2(int, int);
int main()
{
cout << "One." << endl;
thisIsHowYouIfLikeABoss2(9, 9);
cout << "Two." << endl;
thisIsHowYouIfLikeABoss2(4, 9);
return 0;
}
int thisIsHowYouIfLikeABoss2 (int x, int y)
{
cout << "Welcome to the thisIsHowYouIfLikeABoss(), where I calculate if x = y easily." << endl;
if (x = y)
{
cout << "X is Y." << endl;
}
if (x != y)
{
cout << "X is not Y" << endl;
}
}
My compiler is GNU C++ Compiler for Ubuntu,if anyone was wondering.
= is the assignment operator, not the relational equality operator, which is ==.
Change your code to this:
if (x == y)
{
cout << "X is Y." << endl;
}
Protip: if you annotate your function's parameters with const then the compiler would have given you an error with that expression:
int thisIsHowYouIfLikeABoss2( const int x, const int y )
(Unlike in C# and Java, const in C++ does not mean the value is a compile-time fixed-value or literal, so you can use const with variables).
This question already has answers here:
What is a reference variable in C++?
(12 answers)
Closed 8 years ago.
Hi I'm currently learning C++ and I'm trying to pass the value by reference, but I'm having issues with getting the correct output. What seems to be the problem??
void ref(int a)
{
cout << "a = " << a << endl;
a = 1;
cout << "a = " << a << endl;
}
int main()
{
int b = 10;
cout << "b = " << b << endl;
ref(b);
cout << "b = " << b << endl;
return 0;
}
Unless you put void ref(int &a){} you are not actually changing the value of a.
For pass by reference you have to use:-
void ref(int& a) {}
I'm learning C++ and I have trouble with getting recursion working when a function is called by itself.
#include <iostream>
using namespace std;
int countdown(int y) {
if (y==1) {
return 1 && cout << y << endl;
}
else {
return countdown(y-1);
}
}
int main () {
cout << "Countdown from ten: " << endl;
cout << countdown(10) << endl;
}
Of course there are other ways to achieve this, but really I created this example to verify my own understanding of how functions are called recursively.
In the example I added && cout << y to verify if y is being passed to the function as 1, which always appears to be the case irrespective that I call the function as countdown(10).
Could someone tell me if I'm missing something obvious here please?
Your ' cout << y ' only executes if y has been tested to be one.
This version does what I think you want:
#include <iostream>
using namespace std;
int countdown(int y)
{
cout << y << endl;
if (y==1)
{
return 1;
}
else
{
return countdown(y-1);
}
}
int main()
{
cout << "Countdown from ten: " << endl;
cout << countdown(10) << endl;
}
Your call stack looks like this:
main
countdown(10)
countdown(9)
countdown(8)
countdown(7)
countdown(6)
countdown(5)
countdown(4)
countdown(3)
countdown(2)
countdown(1)
std::cout << 1 << std::endl;
If you want to see the whole countdown, move the output command in front of the if condition.
Also, your style of writing the output is very unidiomatic. Note that it only works because 1 %&& cout converts the cout to bool and bool can be converted to int. Please don't write code like that.