English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

void main(){
int x=5;
int y=0;
try{
cout< }
catch{
cout<<"cannot devide by zero";
}

}
//the problem is the catch block

2006-11-12 16:55:34 · 4 answers · asked by Rami 5 in Computers & Internet Programming & Design

answer from the second person
it's right but the program still halts and gives a message box
a try catch should not allow this

2006-11-12 17:49:40 · update #1

4 answers

try - C++ keyword that denotes an exception block (You had this correct)
catch - C++ keyword that "catches" exceptions (you needed to add the variable type parameter)
throw - C++ keyword that "throws" exceptions (You needed to put a throw statement in to activate if you were "dividing by zero"

EDIT: I initialized the divide variable to zero, the program works fine when I compile it, try re-building the solution, if it still doesn't work, I'd have to take a look at what compiler your using (which you didn't mention) to find out what the problem is. OR at least describe your error. Regardless I doubt your compiler would reject integer division, which is the only thing I could think of at the current time that would cause a crash, but I haven't used every compiler yet.

EDIT: I did a minor rewrite of the program, try it now.
#include
using namespace std;
int main()
{
int x=5;
int y=0;
int divide;
try
{
x/y;
throw 1;
}
catch(int a)
{
cout<<"cannot devide by zero" << endl;
system("pause");
return 1;
}
divide = x / y;
cout << divide;
system("pause");
return 0;
}

2006-11-12 17:21:24 · answer #1 · answered by D 4 · 0 1

You need to tell C++ ,the type of exception that you want to catch. This is done as follows :
try{
/** Your try code block goes here */
}
catch(Exception e){
/** Your catch code block goes here */
}

Here Exception is the base exception class(superclass).

Try this, it should work.

2006-11-12 19:43:31 · answer #2 · answered by tutejasaurabh 2 · 0 0

hi, that's only as a wager even though it form of feels that this technique is beginning up in a DOS container, exiting at present day and closing down the DOS container. you're able to desire to place a pause in there whilst this technique is achieved. the appropriate thank you to do this's by ability of including in a decision to getch() previously this technique exits. getch() will examine a single character immediately from the keyboard, with out echoing it to the reveal screen so its appropriate for including a pause on the tip of your software. as an occasion assemble and run right here 2 classes. One is with getch() and the different with out and see the kind. // software a million with out getch() #contain #contain void substantial(void) { printf("nhello with out getch()n"); // This line heavily isn't considered because of the fact the DOS container closes // at present day } // software 2 With getch() #contain #contain void substantial(void) { printf("nhello with a getch()n"); // This line would be considered because of the fact the DOS container continues to be open till // the consumer presses a key // anticipate consumer to press a key getch(); }

2016-10-22 00:05:13 · answer #3 · answered by saggio 4 · 0 0

I think you might try to put the paranthesis () at the end of the catch before braces{...}

Try it and have a good luck.

2006-11-12 17:10:48 · answer #4 · answered by m_Fariz 3 · 0 0

fedest.com, questions and answers