form1.cs

来自「java基础方面的一些实例代码」· CS 代码 · 共 659 行 · 第 1/2 页

CS
659
字号
			this.textBoxCheckResult.Location = new System.Drawing.Point(8, 224);
			this.textBoxCheckResult.Multiline = true;
			this.textBoxCheckResult.Name = "textBoxCheckResult";
			this.textBoxCheckResult.ReadOnly = true;
			this.textBoxCheckResult.Size = new System.Drawing.Size(112, 72);
			this.textBoxCheckResult.TabIndex = 1;
			this.textBoxCheckResult.Text = "";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(8, 120);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(48, 16);
			this.label4.TabIndex = 0;
			this.label4.Text = "结果:";
			// 
			// buttonChecked
			// 
			this.buttonChecked.Location = new System.Drawing.Point(48, 112);
			this.buttonChecked.Name = "buttonChecked";
			this.buttonChecked.Size = new System.Drawing.Size(72, 24);
			this.buttonChecked.TabIndex = 4;
			this.buttonChecked.Text = "checked";
			this.buttonChecked.Click += new System.EventHandler(this.buttonChecked_Click);
			// 
			// buttonUnchecked
			// 
			this.buttonUnchecked.Location = new System.Drawing.Point(48, 144);
			this.buttonUnchecked.Name = "buttonUnchecked";
			this.buttonUnchecked.Size = new System.Drawing.Size(72, 24);
			this.buttonUnchecked.TabIndex = 4;
			this.buttonUnchecked.Text = "unchecked";
			this.buttonUnchecked.Click += new System.EventHandler(this.buttonUnchecked_Click);
			// 
			// buttonUndefine
			// 
			this.buttonUndefine.Location = new System.Drawing.Point(64, 176);
			this.buttonUndefine.Name = "buttonUndefine";
			this.buttonUndefine.Size = new System.Drawing.Size(56, 24);
			this.buttonUndefine.TabIndex = 4;
			this.buttonUndefine.Text = "不指定";
			this.buttonUndefine.Click += new System.EventHandler(this.buttonUndefine_Click);
			// 
			// buttonAssert
			// 
			this.buttonAssert.Location = new System.Drawing.Point(8, 176);
			this.buttonAssert.Name = "buttonAssert";
			this.buttonAssert.Size = new System.Drawing.Size(40, 24);
			this.buttonAssert.TabIndex = 4;
			this.buttonAssert.Text = "断言";
			this.buttonAssert.Click += new System.EventHandler(this.buttonAssert_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(472, 373);
			this.Controls.Add(this.groupBox3);
			this.Controls.Add(this.buttonUnsafeTest);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.groupBox2);
			this.Name = "Form1";
			this.Text = "异常处理演示程序";
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void buttonGenenal_Click(object sender, System.EventArgs e)
		{
			string strTemp = textBoxNumber.Text;
			int x = int.Parse(strTemp);
			strTemp = textBoxDivision.Text;
			int y = int.Parse(strTemp);
			int z = (int)x/y;
			textBoxResult.Text = z.ToString();
		}

		private void buttonException_Click(object sender, System.EventArgs e)
		{
			try 
			{
				string strTemp = textBoxNumber.Text;
				int x = int.Parse(strTemp);
				strTemp = textBoxDivision.Text;
				int y = int.Parse(strTemp);
				int z = (int)x/y;
				textBoxResult.Text = z.ToString();
			}
			catch (DivideByZeroException de)
			{
				MessageBox.Show(de.ToString());
			}
			catch (FormatException se)
			{
				MessageBox.Show(se.ToString());
			}
			//
		}

		private void buttonUnsafeTest_Click(object sender, System.EventArgs e)
		{
			Form2 MyUnsafeRestForm = new Form2();
			MyUnsafeRestForm.ShowDialog(this);
		}

		public void MyThrowExceptionTest()
		{
			string strTemp = this.textBoxInputmcao.Text;
			if (strTemp.Equals("mcao") == true)
				throw (new ArgumentNullException());
			MessageBox.Show(strTemp);
		}

		private void buttonThrowmcao_Click(object sender, System.EventArgs e)
		{
			
			try 
			{
				MyThrowExceptionTest();
			}			
			catch (ArgumentNullException ae)
			{
				MessageBox.Show(ae.ToString()+"  ::" +ae.Message);
			}
			catch (Exception ee) 
			{
				MessageBox.Show(ee.ToString()+"  ::" +ee.Message);
			}
			finally 
			{
				MessageBox.Show("Though you throw a System.ArgumentNullException, I am still mcao!");
			}
		}

		//测试异常嵌套以及Exception的属性
		public void MyThrowExceptionLayer1()
		{
			MyThrowExceptionLayer2();
		}
		public void MyThrowExceptionLayer2()
		{
			MyThrowExceptionLayer3();
		}
		public void MyThrowExceptionLayer3()
		{
			try 
			{
				MyThrowExceptionTest();
			}
			catch (ArgumentNullException ae)
			{
				MessageBox.Show(ae.ToString()+"  ::" +ae.Message);
				throw new  Exception("我就是想输入mcao,你已经套了三层了", ae);
			}
		}

		private void buttonExceptionResult_Click(object sender, System.EventArgs e)
		{
			//
			try
			{
				MyThrowExceptionLayer1();
			}
			catch (Exception ge)
			{
				MessageBox.Show("异常类型:" + ge.ToString());
				MessageBox.Show("异常message:" + ge.Message);
				MessageBox.Show("异常StackTrace:" + ge.StackTrace);
				MessageBox.Show("异常InnerException:" + ge.InnerException);
				MessageBox.Show("异常TargetSite:" + ge.TargetSite);
				MessageBox.Show("异常Source:" + ge.Source);
				MessageBox.Show("异常HelpLink:" + ge.HelpLink);
			}
		}

		//测试自定义的异常类
		public void MyDefinedExceptionTest()
		{
			string strTemp = this.textBoxInputmcao.Text;
			if (strTemp.Equals("mcao") == true)
				throw (new MymcaoException());
			if (strTemp.Equals("cm") == true)
				throw (new MymcaoException("我就是不让你输入mcao或者cm,你能怎么样。"));
			if (strTemp.Equals("jlqiu") == true)
				throw (new MymcaoException("我就是不让你输入mcao或者jlqiu,你能怎么样。", new OverflowException()));
			MessageBox.Show(strTemp);
		}

		private void buttonUserdefinedException_Click(object sender, System.EventArgs e)
		{
			//自定义异常类1
			try 
			{
				MyDefinedExceptionTest(); 
			}
			catch (MymcaoException mcaoe)
			{
				textBoxUserDefinedExceptionResult.Text = mcaoe.Message + " Ha, ha:" +  mcaoe.ToString();
				mcaoe.SendMessage("我就是不让你输入mcao,你能怎么样。");
			}
		}

		private void buttonUserdefinedException2_Click(object sender, System.EventArgs e)
		{
			//自定义异常类2
			try 
			{
				MyDefinedExceptionTest(); 
			}
			catch (MymcaoException mcaoe)
			{
				textBoxUserDefinedExceptionResult.Text = mcaoe.Message + "\n Ha, ha:" +  mcaoe.ToString();
			}
		}

		private void buttonUserdefinedException3_Click(object sender, System.EventArgs e)
		{
			//自定义异常类2
			try 
			{
				MyDefinedExceptionTest(); 
			}
			catch (MymcaoException mcaoe)
			{
				textBoxUserDefinedExceptionResult.Text = mcaoe.Message;
				textBoxUserDefinedExceptionResult.Text += "\n";	
				textBoxUserDefinedExceptionResult.Text += mcaoe.InnerException;
							
			}
		}

		//checked 
		private void buttonChecked_Click(object sender, System.EventArgs e)
		{
			try 
			{
				int i = int.Parse(textBoxInt1.Text);
				int j = int.Parse(textBoxInt2.Text);
				int k;
				checked 
				{
					k = i*j;
				}
				textBoxCheckResult.Text = k.ToString();
			}
			catch (OverflowException oe)
			{
				textBoxCheckResult.Text = oe.Message;
			}
			catch (System.FormatException fe)
			{
				textBoxCheckResult.Text = fe.Message;
			}
		}

		//unchecked
		private void buttonUnchecked_Click(object sender, System.EventArgs e)
		{
			try 
			{
				int i = int.Parse(textBoxInt1.Text);
				int j = int.Parse(textBoxInt2.Text);
				int k = 0;
				unchecked {
					k = i*j;
				}
				textBoxCheckResult.Text = k.ToString();
			}
			catch (OverflowException oe)
			{
				textBoxCheckResult.Text = oe.Message;
			}
			catch (System.FormatException fe)
			{
				textBoxCheckResult.Text = fe.Message;
			}			
		}

		private void buttonUndefine_Click(object sender, System.EventArgs e)
		{
			try 
			{
				int i = int.Parse(textBoxInt1.Text);
				int j = int.Parse(textBoxInt2.Text);
				int k = i*j;
				textBoxCheckResult.Text = k.ToString();
			}
			catch (OverflowException oe)
			{
				textBoxCheckResult.Text = oe.Message;
			}
			catch (System.FormatException fe)
			{
				textBoxCheckResult.Text = fe.Message;
			}			
		}

		private void buttonAssert_Click(object sender, System.EventArgs e)
		{
			//调用自定义断言
			string strTemp = this.textBoxInputmcao.Text;
			try 
			{
				ExAssert.MyAssert((strTemp.Equals("mcao") == false), "Assert--仍然不允许你输入mcao", new Exception());
				textBoxCheckResult.Text = strTemp;
			}
			catch (Exception ee) 
			{
				strTemp = ee.ToString();
				strTemp += "\n"+ee.Message;
				strTemp += "这一次我使用断言,但我还是不让你输入mcao,你能怎样?";
				MessageBox.Show(strTemp);
			}
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?