Posts

Showing posts with the label try-catch

Variable may be uninitialized/is used uninitialized

Variable may be uninitialized/is used uninitialized I'm writing an English-Metric converter program in C++, and I'm trying to use try, throw, catch to reject negative/non-numeric values in the 'main' function. My two problems are: 1.) Whenever I enter, say, 'g' into the console, I get an output: 0 inches is equal to 0 centimeters AND THEN I get the error display that I want to pop up. What i need is only the error display to be output. 2.) When I enter a negative number, like -3, I get the proper conversion as a negative number when I would like it to tell me my input is invalid. Here is my code. #include <iostream> #include <cctype> char menuSelect(); using namespace std; int main(int argc, const char * argv) { double inches; double centimeters; char select; try { do { if (centimeters < 0.0 || inches < 0.0) throw 0; if (!cin) throw 0; select...

Try / catch with a private method [closed]

Try / catch with a private method [closed] I am trying to call a method that is private inherited from the super class (I KNOW THAT YOU CAN NOT CALL IT IN MAIN BECAUSE IT IS PRIVATE) However, I am trying to use a try catch statement that keeps running my program and give me an exception saying "This method cannot be Called because it is private" (EXAMPLE) this is what I have on my try / catch public int num1 = 3; try { superClass.methodOne(num1); } catch(Exception e) { System.out.printf("%s","ERROR: methodOne cannot be executed! IT IS PRIVATE"); } This is the error that it gives me! error: methodOne(int) has private access Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the que...