📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CodeForChapter4cs_DLL;
using System.Diagnostics;
using System.IO;
using System.Collections;
using System.Reflection;
namespace CodeForChapter4cs
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// bool b;
try
{
}
catch (Exception)
{
//ex.StackTrace
throw;
}
string s = "start";
for (int i = 0; i < 30; i++)
{
s += "let's kill our perf";
}
this.ReturnSomething();
}
public int ReturnSomething()
{
return 5;
}
private void menuItem1_Click(object sender, EventArgs e)
{
//this.SomeMethod();
this.AnotherMethod(new SomeClass());
}
private void SomeMethod()
{
// 1. let any exception bubble up
this.MethodThatMayThrow();
// 2. rethrow
try
{
this.MethodThatMayThrow();
}
catch (Exception)
{
// do something with the exception object
// or do something else as cleanup in this code block
throw; //rethrows
}
// 3. Catch exception
try
{
this.MethodThatMayThrow();
}
catch (Exception)
{
// FULLY deal with this exception
//ex.st
}
// 4. Swallow
try
{
this.MethodThatMayThrow();
}
catch (Exception)
{
// swallow, just so it doesn't go up to other methods
// DO NOT do this!
}
}
private void MethodThatMayThrow()
{
throw new Exception("The method or operation is not implemented.");
}
private void AnotherMethod(SomeClass obj)
{
try
{
((ISomeInterface)obj).DoIt();
}
catch (InvalidCastException ex)
{
// log the exception to file
Debug.WriteLine("The stacktrace: \r\n" + ex.StackTrace);
// take some real recovery action!
}
}
private void menuItem2_Click(object sender, EventArgs e)
{
SomeType obj = null;
obj.SomeMethod(); // NullReferenceException
}
private void button1_Click(object sender, EventArgs e)
{
//this.DoSomethinginterestingWith(@"\testFile.txt");
ArrayList ar=null;
//ar.
ar.Capacity = 1;
this.WorkWithSomeList(ar);
}
private void DoSomethinginterestingWith(string path)
{
try
{
FileStream fs = File.Open(path, FileMode.Open); //FileNotFoundException
// do something interesting with fs
}
catch (FileNotFoundException ex)
{
// TODO deal with the invalid input
}
}
//private void DoSomethinginterestingWith(string path)
//{
// if (File.Exists(path))
// {
// FileStream fs = File.Open(path, FileMode.Open); //FileNotFoundException
// // do something interesting with fs
// }
// else
// {
// // TODO deal with the invalid input
// }
//}
//private void AnotherMethod(SomeClass obj)
//{
// ISomeInterface i = obj as ISomeInterface;
// if (i != null)
// {
// ((ISomeInterface)obj).DoIt();
// }
// else
// {
// // Do something else!
// }
//}
private void WorkWithSomeList(ArrayList ar)
{
ar.Add(new object());
if (!(ar.IsFixedSize || ar.IsReadOnly))
{
}
else
{
// do some sensible alternative
}
}
private void button2_Click(object sender, EventArgs e)
{
Type t = this.GetType();
MethodInfo m = t.GetMethod("DoIt");
m.Invoke(this, null);
}
public void DoIt()
{
throw new InvalidOperationException("my msg");
}
}
public class SomeType
{
public void SomeMethod()
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -