zapshe wrote: |
---|
Your catch block isn't working because you're not throwing anything at it. Line 42 - you simply typed throw; - Even though catch(...) doesn't have a specific type of throw to catch, you still need to actually throw something. simply make it throw 1; or throw "You Screwed Up!"; and it'll work. The whole point of throwing exceptions is so you can handle them. For example, lets say you made a calculator. If the power inputs random letters, you don't want your program to crash or fail. Instead, check if the input is invalid. If so, throw an exception. However, there might be multiple reasons for the input to be wrong, such as letters, invalid operator, dividing by 0, etc.. So you'll have multiple cases where you'd want to throw an exception. My calculator example isn't amazing, but as a program becomes more complex with more room for epic errors, you'll want to be able to catch them and handle them effectively. |
throw;
would work.throw 1;
but it is still not working. Here's the new code: http://cpp.sh/457wb. To see what I'm seeing, run it and input something containing letters like: "v1.13"
PiggiesGoSqueal wrote: |
---|
Although I still don't fully understand why they're so useful. But probably because I haven't made any very complex programs. For your calculator example, couldn't you just get the user's input, then with if statements |
For your calculator example, couldn't you just get the user's input, then with if statements and maybe for loops, check if the input has any issues right after they input the info then do your calculations? |
int* arr = new int[1000];
Peter87 wrote: |
---|
C++ Shell doesn't seem to show the output to std::cerr anywhere. If I change to cerr to cout, or run it locally, it does show the message. |
zapshe wrote: |
---|
Yea, you could. Not my best example. look at something like this:int* arr = new int[1000]; If "new" fails to allocate memory for this, it'll throw an exception. In order to catch an error from this, you have to use try and catch (or make it new(nothrow) but why bother). |
|
|
9:22: warning: missing terminating " character 9:9: error: missing terminating " character In function 'int main()': 6:14: warning: unused variable 'arr' [-Wunused-variable] 10:5: error: expected primary-expression before '}' token |
Cubbi wrote: |
---|
Imagine when the input was taken from the user, the code makes a function call, that function makes another function call.. and 50 calls deep, there's finally a function that realizes that the input was invalid and it can't proceed. It could do an if statement, return an error code, the caller would have to do an if statement, return the error code, etc. 50 levels of nothing but check for error and if there is one, undo any work done so far and return error to the caller. With exceptions, there is one throw where the error was detected, and one try/catch where the problem could be handled, with no extra code anywhere inbetween. Real example: When you open a file in Notepad++, it starts reading it, as it reads it, it calls functions that prepare lines of text for drawing, choose fonts, apply highlighting, etc, and then one of these functions, maybe not 50 but more than 10 levels deep, in the text manipulation library called scintilla, runs out of memory allocating another word to display. It throws the exception std::bad_alloc, which undoes everything done so far in that function, and in its caller, and in its caller, until it reaches the try/catch clause in the Notepad++ function that asked the user for the file name to open, and the catch clause pops up the message saying the file was too large to open. |
Although it doesn't throw anything. Not sure where I'd put the "throw" word. |
|
|
Ganado wrote: |
---|
As the user of the code, you don't need to write "throw". You're using what's already been designed; that the new operator, by default, can throw std::bad_alloc. |
zapshe wrote: | ||
---|---|---|
"new" will throw an exception if you try to allocate too much:
Try it out yourself. Catching the exception and handling it is a lot better than having your program crash. |
Basic Structure Behind Project: - take user input or a file - store data into program (using a class) - compare user's inputted data to the game's official data (in JSON files) - then do some calculations - output the result to the user |
zapshe wrote: |
---|
Usually at this point, the thread will likely die out as no one will answer the first question so I'll try to. |
zapshe wrote: |
---|
The best way to organize it is to make every one of those goals up there into it's own function for easier editing. In my opinion, using 3 files would making editing harder for smaller programs, since there are now 3 different files! If the program isn't that big, consider having it all in one .cpp. |
zapshe wrote: |
---|
Unless they specifically told you to use different files, separate functions should be enough organizing I'd think. |
zapshe wrote: |
---|
Visual studio can be slow, but a good CPU paired with an SSD makes the wait time minimal. I myself have the main portion of the program on the SSD and other bigger parts of it installed on my HDD and it runs smoothly. Consider an upgrade perhaps. |
-Wall -Wextra -Wshadow -Wnon-virtual-dtor -pedantic
When to Use Try/Catch? |
1. What is the best way to organize my code for the program? |
Shop
, Item
and Parser
.if I have a LOT of files, it becomes much more to type. |
zapshe wrote: |
---|
If it's your own project, then experiment and delve into it, find what you like to do and what feels best. A project's size and complexity usually determines if you need to go up and beyond with extra files. Use extra files and test things out. Either you'll like it or hate it. Maybe you'll hate it, try to make it into 1 file, then realize it was better the other way! See what you like and feel is more convenient. |
zapshe wrote: |
---|
And Visual Studio is a full blown IDE, so it'll be slower than VSC. Your setup seems to be more than enough. On my setup, Visual Studio usually only takes a few seconds to compile and run my code, 5 seconds at most I'd say. After you compile once, if you're simply editing your code, it'll only compile the changes and use the already compiled data from the last time to save time. |
zapshe wrote: |
---|
I know that there are ways to do things like flags in Visual Studio through the project settings, but I don't know much about them. I've never really had to mess with that. Only time I needed to use a flag was in my CS class since they use Linux. However, I doubt there's anything that those flags do that you can't do in Visual Studio. |
coder777 wrote: |
---|
The first thing for successfull programming is imho descriptive names. The next think is 'divide and conquer'. |
coder777 wrote: |
---|
For your project it would mean (roughly): Make a class game, player, shop, and may be item. Whether you make a file for each depends on the amount of code for each. You can make the file later when it grows. With this structure you can start designing: The game contains player/shop. The shop contains item. etc. |
coder777 wrote: |
---|
For json I suggest to use boost. More precisely property_tree: https://www.boost.org/doc/libs/1_70_0/doc/html/property_tree.html They provide parser for other formats. For initializing your program the INFO format is very good... |
coder777 wrote: |
---|
When to Use Try/Catch? Don't. It's a nightmare when it comes to program flow. |
dhayden wrote: | |
---|---|
I usually organize project files like this: Class1.h & Class1.cpp // files for Class1 Class2.h & Class2.cpp // files for Class2 ... misc.h & misc.cpp // miscellaneous functions that don't belong an a particular class main.cpp |
|
|
dhayden wrote: | |
---|---|
If you're using g++ then you probably have make (or gmake) installed on your system. Create a makefile and all your troubles will go away. |
dhayden wrote: |
---|
It's important to separate the declaration of the class from the definition of the methods because you might need to #include the .h file with the declaration in many other files. If you had the definitions in the header file then you'd end up with multiply defined methods. |
dhayden wrote: |
---|
Regarding make and makefiles, google "makefile tutorial" and see how to create a makefile. The idea is that if you properly setup the makefile, then after modifying any of the code, you just have to type "make" and it will recompile only what must be recompiled. |
Also, why do you suggest using the INFO parser rather than the JSON parser? I am parsing JSON files after all. |
Haha, well, now I've been told opposite suggestions. :P Idk, I think I'll give it a try if I come across a situation that would require that std::bad_alloc (or something) setting. But other than that I probably won't use it. |
coder777 wrote: | |
---|---|
I do not suggest using the INFO parser for your particular need. But it is useful for other things like initial data for your program. The point is that all these formats leads to one associative container: ptree, which is easy to deal with.
Actually the nature of exception handling is jump out of the middle of a function into the middle of another function more or less unexpectedly. That makes it hard to impossible to follow the program flow. It means: The fewer exceptions the more stable your program will be. |