Exception

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

double division(int a, int b) {
   if( b == 0 ) {
      throw "Division by zero condition!";
   }
   return (a/b);
}

int main () {
   int x = 50;
   int y = 0;
   double z = 0;
 
   try {
      z = division(x, y);
      cout << z << endl;
   } catch (const char* msg) {
     cerr << msg << endl;
   }

   return 0;
}

In catch (const char* msg), why must I use const char* variable??
Why I cant just use string?
Because when I use other variable the output will always be
terminate called after throwing an instance of 'char const*'

Why must I use const char*?

Thanks..
Implicit type conversion doesn't happen for primitive types in exceptions.

See also, ISO C++'s suggestions for exceptions.
https://isocpp.org/wiki/faq/exceptions#what-to-throw
They do not suggest throwing primitive types.

https://stackoverflow.com/questions/6248404/c-exceptions-is-throwing-c-string-as-an-exception-bad

As an aside, exceptional scenarios, such as low memory, it's safer to throw a const char* literal (or a wrapper around it) than construct a string.
Last edited on
I do not intend to be anti-curious, but it may waste your valuable time if you keep asking youself these kinds of questions, perhaps it would be more appropriate to accept the fact, and move on.

Adding to @Ganado 's post, it is logical. A char is a single byte, a string literal is implicitly null-terminated, so it will take up one more byte than the observable number of characters in the literal.

Last edited on
I do not intend to be anti-curious, but it may waste your valuable time if you keep asking youself these kinds of questions, perhaps it would be more appropriate to accept the fact, and move on.


What a ridiculous thing to say. Understanding why a thing does, or doesn't, work can lead to a greater understanding of the language, and of programming techniques. It's good to ask questions, and come to a deeper understanding of the language.

Frankly, many of your posts here look very much like trolling. Is that really how you want to be perceived?
Frankly, many of your posts here look very much like trolling. Is that really how you want to be perceived?
There is some context between myself and Darkparadox out of the forum, knowing his ability I feel as though he just should master the fundementals first.
H

How on earth am I trolling? http://www.cplusplus.com/forum/beginner/257033/

I just misread the .ico issue, please, we are delaying the forum, please PM those you despise
Last edited on
Vilch wrote:
No need for an entire function, just use
#define rand() rand() % 6 + 1
Well this, for one, is a horrible, error-prone suggestion.
Last edited on
Well this, for one, is a horrible, error-prone suggestion.

But it works... In this case anyway, that was the only ever reason he had to use the line of code
Last edited on
@Vilch, Can you list all of the things wrong with it?
I don't understand, you're taking the line out of context. Clearly functions when no other random tasks are needed. Click. the. link. and. run.

http://cpp.sh/5dwsn
no. thanks.
I am very sorry. But it literally works in the context. If taken out of context this line is not ideal, but if you was compiling a program where only rand()%3 was being used. A similar line would be IDEAL.
#define exterminate certainperson/0
Last edited on
Vilch wrote:
I am very sorry

Yes.
Yes you are.
What? Ofcourse, I just used imperative against you...





Now I will strangely say a country.
Holland.

Hmm, an adrift word aswell.

Flemish
Last edited on
sure, it works. lots of horrible things work.

you overwrote the standard rand(), which stops rand() from working as expected if you expand your code to do more stuff, and it would be totally unclear why.
it uses rand(), which is outdated/C code, instead of random.
it uses a macro function, which is difficult to debug / trace / deal with if it malfunctions. This is compounded by the overwrite of the standard one, making debugging it when you DO have an issue unpossible.
nitpicking, it has 2 magic numbers in one line.

I get it. I go cryptic myself at times. that does not make it 'IDEAL', even if it runs like greased lightning and gets the job done. I just rolled my own int to text because the built in ones were the bottle neck in a 100 million at a time problem I was dealing with. It went from 2 sec / million to 0.4 with just that ugly little C routine. Its quick, but I know its not 'IDEAL'; no one else will be able to read it or mess with it etc due to general cryptic gibberish code. I don't care, it gets my data punched thru. If you can justify your gibberish, go for it. But even if you can justify this atrocity, you should at the very least not over-write the original rand() and call yours something simple like rand6() or something. that one change would make this significantly better at no real cost to you. For another 5 min of effort you could use the C++ library. Make it a normal function instead of a macro unless you can prove its faster and you need the speedup from the forced inline expansion. With those small changes, it would be a ton cleaner.

Last edited on
call yours something simple like rand6() or something. that one change would make this significantly better at no real cost to you.
I kept saying,

If taken out of context this line is not ideal,
In his its fine, because it doesn't matter what it's called, it will always be rand()%6+1 But if he edited the program and affected this, then you are correct

Right. We all get that. If all code would just cooperate and stay 20 or so lines long and never be edited, updated, or re-used, life would be a lot simpler. Unfortunately, anything worth writing tends to grow, and if it had a hidden gotcha, some poor sap has to figure it out. All for want of adding 1 more letter to a name.
some poor sap has to figure it our
“Experience is the most brutal of teachers but you learn, my God, do you learn”

― CS Lewis
Last edited on
Well said jonnin.

As Scott Meyers puts it, the most important design guideline is:

Make interfaces easy to use correctly and hard to use incorrectly.


https://www.aristeia.com/Papers/IEEE_Software_JulAug_2004_revised.htm
Last edited on
Scott Meyers


It's a shame his audiences only gets about a quarter of his jokes. He's such a unique person in tech, sad to see him retire from C++.
Topic archived. No new replies allowed.