The following code should add integers from two TextBoxes and display the result in txtResult. Assume that intValue1 and intValue2 are declared as ints. Find the error(s) in the following code:


try
{
intValue1 = Int32.Parse( txtInput1.Text );
intValue2 = Int32.Parse( txtInput2.Text );

txtOutput.Text = Convert.ToString( intValue1 + intValue2 );

catch ( )
{
MessageBox.Show(
"Please enter valid ints.",
"Invalid Number Format",
MessageBoxButtons.OK, MessageBoxIcon.Error );
}
}


There are two errors in this code. First, the catch block appears inside the try

block. All catch blocks must follow immediately after the try block. Second, you will need to specify which type of exception is being caught between the parentheses after the catch statement (in this case, a FormatException). The complete incorrect code reads:





// Addition.cs (Incorrect)



using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;



namespace Addition

{

///

/// Summary description for FrmAddition.

///


public class FrmAddition : System.Windows.Forms.Form

{

// Label and TextBox for first value

private System.Windows.Forms.Label lblValue1;

private System.Windows.Forms.TextBox txtInput1;



// Label and TextBox for second value

private System.Windows.Forms.Label lblValue2;

private System.Windows.Forms.TextBox txtInput2;



// Label and TextBox for output

private System.Windows.Forms.Label lblProduct;

private System.Windows.Forms.TextBox txtOutput;



// Button to perform addition

private System.Windows.Forms.Button btnAdd;



///

/// Required designer variable.

///


private System.ComponentModel.Container components = null;



public FrmAddition()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();



//

// TODO: Add any constructor code after InitializeComponent

// call

//

}



///

/// Clean up any resources being used.

///


protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}



// Windows Form Designer generated code



///

/// The main entry point for the application.

///


[STAThread]

static void Main()

{

Application.Run( new FrmAddition() );

}



// handles btnAdd's Click event

private void btnAdd_Click(

object sender, System.EventArgs e )

{

int intValue1;

int intValue2;



try

{

intValue1 = Convert.ToInt32( txtInput1.Text );

intValue2 = Convert.ToInt32( txtInput2.Text );



txtOutput.Text =

Convert.ToString( intValue1 + intValue2 );



catch ( )

{

MessageBox.Show(

"Please enter valid integers.",

"Invalid Number Format",

MessageBoxButtons.OK, MessageBoxIcon.Error );

}



}



} // end method btnAdd_Click



} // end class FrmAddition

}







The complete corrected code should read:



// Addition.cs (Correct)



using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;



namespace Addition

{

///

/// Summary description for FrmAddition.

///


public class FrmAddition : System.Windows.Forms.Form

{

// Label and TextBox for first value

private System.Windows.Forms.Label lblValue1;

private System.Windows.Forms.TextBox txtInput1;



// Label and TextBox for second value

private System.Windows.Forms.Label lblValue2;

private System.Windows.Forms.TextBox txtInput2;



// Label and TextBox for output

private System.Windows.Forms.Label lblProduct;

private System.Windows.Forms.TextBox txtOutput;



// Button to perform addition

private System.Windows.Forms.Button btnAdd;



///

/// Required designer variable.

///


private System.ComponentModel.Container components = null;



public FrmAddition()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();



//

// TODO: Add any constructor code after InitializeComponent

// call

//

}



///

/// Clean up any resources being used.

///


protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}



// Windows Form Designer generated code



///

/// The main entry point for the application.

///


[STAThread]

static void Main()

{

Application.Run( new FrmAddition() );

}



// handles btnAdd's Click event

private void btnAdd_Click(

object sender, System.EventArgs e )

{

int intValue1;

int intValue2;



try

{

intValue1 = Int32.Parse( txtInput1.Text );

intValue2 = Int32.Parse( txtInput2.Text );



txtOutput.Text =

Convert.ToString( intValue1 + intValue2 );

}



catch ( FormatException formatExceptionParameter )

{

MessageBox.Show(

"Please enter valid integers.",

"Invalid Number Format",

MessageBoxButtons.OK, MessageBoxIcon.Error );

}



} // end method btnAdd_Click



} // end class FrmAddition

}



Computer Science & Information Technology

You might also like to view...

The link between a file type and its default program is called a(n) ____.

A. association B. connection C. attachment D. library

Computer Science & Information Technology

An _________ authenticates the sender of a document.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

Who should be on the disaster recovery planning team?

A) People who have a fundamental understanding of what it takes to get back to business as usual B) The same people involved in the actual recovery effort should a disaster be declared C) The board of directors D) The same people who will go to disaster recovery drills

Computer Science & Information Technology

The operating system manages each and every piece of hardware and software.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology