Try Catch block showing wrong error message - try-catch

Facing an issue in Dev C++ wherein the catch block is not showing the desired error message
#include<iostream>
using namespace std;
void mightGoWrong()
{
bool error1;
bool error2;
if(error1)
{
throw "Issue encountered!!";
}
}
int main(void)
{
try
{
mightGoWrong();
}
catch(int e)
{
cout << "Error Code is: "<<e<<endl;
}
cout<<"Still running"<<endl;
}
The message is get is : Still Running.Need to know what i am doing wrong

You need to initialize your boolean variable before your code starts working.
It is considered best practice to always give your variables some default value. Your error1 value has no value assigned to it, therefore throw error never gets to complete its job.
Also you are throwing a string, so you need to catch a string not an int.
void mightGoWrong()
{
bool error1=true;
bool error2=true;
if(error1)
{
throw "Issue encountered!!";
}
}
int main(void)
{
try
{
mightGoWrong();
}
catch(string e)
{
cout << "Error Code is: "<<e<<endl;
}
cout<<"Still running"<<endl;
}

Related

C# Array out of bounds

I have created for loop but it only executes once instead of multiple times. At first it takes value but after increment ment it doesnot executes but catch an exception of array out of bounds and indexoutofexception.
Why this for loop is not repeating, I can't seem to find its solution.
This is my code
private void CheckForVariable()
{
//to split parameter by , then stored in array
parameter = inputcommand[1].Split(",".ToCharArray());
for (int i = 0; i < parameter.Length ; i++)
{
try
{
//checks if the parameter is an int
int test = Convert.ToInt32(parameter[i]);
//Console.WriteLine(parameter[i]);
}
catch
{
Boolean foundVariable = false;
if (foundVariable == false)
{
foreach (KeyValuePair<string, int> variable in VariableDictionary)
{
if (variable.Key.Equals(parameter[i]))
{
parameter[i] = variable.Value.ToString();
}
}
}
}
}
}
I tried to do exception handling using try catch, it doesnot seems to work. It is only taking single value instead of multiple times. I want to pass repeated value until it executes as expected.

How to continue the loop even it catches error ArrayIndexOutOfBounds?

If top==size, it catches an error and displays message Stack is full without stopping the loop .how to do that? But my code below will not stop
here is my code...
while(ask==true){
try{
String input=JOptionPane.showInputDialog("1.Push? Just type yes 2. Pop? just type no");
if(input.equals("yes")){
String element=JOptionPane.showInputDialog("Enter Element");
int num=Integer.parseInt(element);
top++;
arr[top]=num;
System.out.println("Insertion was successful:"+" "+arr[top]);
// isFull();
}else if(input.equals("no")){
pop();
}if(input.equals("exit")){
getStack();
top();
ask=false;
}
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("full");
}
}
while(ask==true) {
try {
String input=JOptionPane.showInputDialog("1.Push? Just type yes 2. Pop? just type no");
if (input.equals("yes")){
String element=JOptionPane.showInputDialog("Enter Element");
int num=Integer.parseInt(element);
top++;
if (top == size) throw new ArrayIndexOutOfBoundsException(); //solution
arr[top]=num;
System.out.println("Insertion was successful:"+" "+arr[top]);
// isFull();
} else if (input.equals("no")){
pop();
} if (input.equals("exit")){
getStack();
top();
ask=false;
}
} catch (ArrayIndexOutOfBoundsException e){
System.out.println("full");
}

WPF. Textbox_textchanged not working for the last character, refuses to subtract last integer

I´ve got a small issue with my textbox-event...
When entering a number everything works fine and the integer in the textbos is subtracted from my sum that i have as an property in another class, but when I am deleteing the same number the last singular refuses to be subtracted from my variable that is connected to a label. I have to write 0 in the textbox to set the my label to my property.
If someone could helt me out I would be really gretfull!
private void txtBoxDiscount_TextChanged(object sender, TextChangedEventArgs e)
{
try
{
if (inv.TotalAllItems > Int32.Parse(txtBoxDiscount.Text))
{
float temp = 0;
temp = inv.TotalAllItems - Convert.ToInt32(txtBoxDiscount.Text);
lblTotalAmount.Content = temp;
}
else
{
MessageBox.Show("Discount can not be greater than the total amount.\nPlease try again", "Something went wrong", MessageBoxButton.OK, MessageBoxImage.Hand);
txtBoxDiscount.Text = "";
lblTotalAmount.Content = inv.TotalAllItems;
}
}
catch
{
}
}
Put a breakpoint in the empty catch block. You'll find that Int32.Parse(txtBoxDiscount.Text) throws an exception when txtBoxDiscount.Text is an empty string. This is why you should never use an empty catch block.
Use Int32.TryParse() instead. If the text can't be parsed as a number, it will leave discount equal to zero.
private void txtBoxDiscount_TextChanged(object sender, TextChangedEventArgs e)
{
int discount = 0;
Int32.TryParse(txtBoxDiscount.Text, out discount);
if (inv.TotalAllItems > discount)
{
float temp = 0;
temp = inv.TotalAllItems - discount;
lblTotalAmount.Content = temp;
}
else
{
MessageBox.Show("Discount can not be greater than the total amount.\nPlease try again", "Something went wrong", MessageBoxButton.OK, MessageBoxImage.Hand);
txtBoxDiscount.Text = "";
lblTotalAmount.Content = inv.TotalAllItems;
}
}
If you want to treat a non-numeric string as an error, rather than as zero, Int32.TryParse() also returns false when the parse fails:
private void txtBoxDiscount_TextChanged(object sender, TextChangedEventArgs e)
{
int discount = 0;
if (!Int32.TryParse(txtBoxDiscount.Text, out discount))
{
// Do something else
}
else if (inv.TotalAllItems > discount)
{

How to use fork() to iterate/search through a 3D/multi dimension int array

Hi guys I'm trying to get some practice out using fork() and with multi dimention arrays.
so i'm trying to tell my program to go through a 3D array where I assigned it at specific locations a certain number and to search through the whole array for that specific number and give me back where it found it.
now I want to use fork() just for practice sake for this. the compiler is giving me a bunch of errors related to both the array itself and the fork()
this what i got so far:
int i,j,k;
int this[100][100][100];
int charlie; //charlie is what i called the result of the a iteration
this[30][2][4]=23;
this[20][1][3]=23;
this[80][19][90]=23;
void ChildProcess()
{
printf("child\n");
for (i=50;i<75;i++)
{
for(j=50;j<75;j++)
{
for(k=50;k<75;k++)
{
charlie=this[i][j][k];
if(charlie==23)
{
printf("i=%d,j=%d,k=%d",i,j,k);
}
}
}
}
}
void ParentProcess()
{
printf("parent\n");
for (i=0;i<50;i++)
{
for(j=0;j<50;j++)
{
for(k=0;k<50;k++)
{
charlie=this[i][j][k];
if(charlie==23)
{
printf("i=%d,j=%d,k=%d",i,j,k);
}
}
}
}
}
void ChildProcess()
{
printf("child2\n");
for (i=75;i<100;i++)
{
for(j=75;j<100;j++)
{
for(k=75;k<100;k++)
{
charlie=this[i][j][k];
if(charlie==23)
{
printf("i=%d,j=%d,k=%d",i,j,k);
}
}
}
}
}
int main()
{
int pid=fork();
printf ("this is the pid %d\n",pid);
if(pid!=0)
{
ParentProcess();
}
else
{
ChildProcess();
}
int pid2=fork();
if (pid==0)
{
ChildProcess2();
}
}
one of the errors it gave me was:
s.c:7:1: warning: data definition has no type or storage class [enabled by default] this[30][2][4]=23;
and
s.c:5:5: note: previous declaration of ‘this’ was here int this[100][100][100];
these 2 errors keep popping up non stop.
to be honest I'm not sure what it means since i didnt reclare it or smt

Segmentation fault

In C, when I try to run this program, I get a "Segmentation fault". What does it mean? How can I fix this?
Tag tagNewDataPoint(const double x[MAX_DIMENSION],
const double w[MAX_DIMENSION],
const int d)
{
int separator_arr,point_arr;
double result = 0;
for (separator_arr=0;separator_arr<d;separator_arr++)
{
for (point_arr=0;point_arr<d;separator_arr++)
{
result += w[separator_arr]*x[point_arr];
}
}
if (result <0)
{
return NEG;
}
else if (result >0)
{
return POS;
}
else
{
return NOTAG;
}
}
This:
for (point_arr=0;point_arr<d;separator_arr++)
should be:
for (point_arr=0;point_arr<d;point_arr++)
You increment the separator_arr, but checks the pointer_arr value (which is never changed) soon enough separator_arr is too big, and your address is invalid.
You have index crosstalk.
for (point_arr=0;point_arr<d;separator_arr++)
should be
for (point_arr=0;point_arr<d;point_arr++)

Resources