I have just converted from Java and am trying to start learning by translating some of my old code. I want to write a FileHandler class that can read/write strings and objects to files. I am starting out with writing and reading text to/from a file.
Firstly, the try-catch:
In Java a function can be declared "throws exceptions", thus telling the compiler that when used, the function must be surrounded with try-catch. Is there anything similar in c++?
And is the use of catch(exception e) the same as catch(...)? (in Java catch(Exception e) catches every possible exception)
Secondly, input:
I want to open myFile using a string, but when doing this I can not compile my program!
In Java a function can be declared "throws exceptions", thus telling the compiler that when used, the function must be surrounded with try-catch. Is there anything similar in c++?
Yes but this feature is deprecated
And is the use of catch(exception e) the same as catch(...)? (in Java catch(Exception e) catches every possible exception)
In C++ you can throw any type, not only std::exception instances. catching a std::exception will catch only those, catching an ellipsis will catch everything
I want to open myFile using a string, but when doing this I can not compile my program!
on the current standard file streams constructors take C string arguments. To get a C string from a C++ string call str.c_str() ( this is going to be fixed with the next version of C++ )
because you can't know everything that may be thrown by the function called within your function.
and how else should one do this?
static string readFile(const string url) throw ( exception )
On some compiler it will have no effect. If you omit that means that the function can throw anything