如何處理例外錯誤狀況 應用介紹-[try-catch-finally]
1.) NG程式
//錯誤程式,程式直接當掉(原因被除數不得維0)
private void button1_Click(object sender, EventArgs e)
{
int a = 0;
int b =100/a;
Console.WriteLine("b is {0}", b);
Console.WriteLine("End of job ");
}
2.) 攔截NG程式
//try-catch-finally語法(程式不會當掉,但提報錯誤)
private void button2_Click(object sender, EventArgs e)
{
int a = 0;
try
{
int b = 100 / a;
Console.WriteLine("b is {0}", b);
}
//錯誤攔截訊息
catch (Exception err1)
{
Console.WriteLine("Divided by zero ");
Console.WriteLine("Divided by zero {0}",err1 );
MessageBox.Show(err1.ToString ());
}
//最後一定會執行
finally
{
Console.WriteLine("End of job");
}
3.) 自行判斷NG錯誤跳轉攔截錯誤訊息(throw 使用)
//自行判斷錯誤跳轉攔截錯誤訊息(程式不會當掉,但提報錯誤)
private void button3_Click(object sender, EventArgs e)
{
int a = 100, b = 0;
try
{
if (b == 0)
{
//跳轉呼叫丟出錯誤
throw new ArithmeticException("Div by zero");
}
else
{
int c = a / b;
Console.WriteLine("Resoult is " + c);
}
Console.WriteLine("After divide2");
}
//
catch (ArithmeticException error)
{
Console.WriteLine("Caught!{0}",error );
}
===========================================
自訂錯誤類別-待續
2010年1月26日 星期二
2010年1月19日 星期二
[c#] String字串處理Split 應用介紹-[Split]
String字串處理Split 應用介紹-[Split]
//split
private void button_Click(object sender, EventArgs e)
{
char[] del = new char[] { ',','/' }; //多符號分離
//char del = ','; //單符號分離
string line = "datalog_mo1_0,2010/1/19下午 01:47:52,691,1,40197574905.7755";
string[] split = line.Split(del);
foreach (string s in split)
{
listBox1.Items.Add(s); //送到目標 listbox檢視
}
}
//split
private void button_Click(object sender, EventArgs e)
{
char[] del = new char[] { ',','/' }; //多符號分離
//char del = ','; //單符號分離
string line = "datalog_mo1_0,2010/1/19下午 01:47:52,691,1,40197574905.7755";
string[] split = line.Split(del);
foreach (string s in split)
{
listBox1.Items.Add(s); //送到目標 listbox檢視
}
}
2010年1月2日 星期六
[SIEMENS SCL FC ] 由函式FC回傳演算計算 應用介紹-[SCL-FC ][IEC 61131-3]
由函式FC回傳演算計算 應用介紹-[SCL-FC][IEC 61131-3]
FUNCTION SQUARE : INT //帶回傳值
FUNCTION SQUARE : INT //帶回傳值
(*********************************************************
This function returns as its function value the square of the
input value or if there is overflow, the maximum value that
can be represented as an integer.
***********************************************************)
//know_how_protect //程式保護
//變數宣告in類型
VAR_INPUT
value : INT;
END_VAR
//程式開始
BEGIN
IF value <= 181 THEN
SQUARE := value * value; //計算含式
ELSE
SQUARE := 32_767; // overflow, 設定最大值並回傳當成錯誤碼
END_IF;
END_FUNCTION
2009年12月27日 星期日
[c#] 單一執行緒與委派 應用介紹-[Thread and Delegate]
單一執行緒與委派 應用介紹-[Thread and Delegate]
//產生紅色球球
bool flg1, flg2,flg3;
private void button1_Click(object sender, EventArgs e)
{
flg1 = true;
Thread th1 = new Thread(readtst);
th1.Start();
}
//執行緒目標
public void readtst()
{
while (flg1 == true)
{
textBox1.BeginInvoke(new creatball(rdball ));
Thread.Sleep(10);
}
}
//委派目標 (執行緒委派目標值傳回到form的物件)
private delegate void creatball( );
private void tst()
{
textBox1.AppendText("read ball" +DateTime .Now .ToLongTimeString ()+ "\n");
}
//委派目標 - 產生紅色球球
public void rdball()
{
Ball aBall;
aBall.pt = new Point(rd.Next(20, this.ClientSize.Width - 20), rd.Next(40, this.ClientSize.Height - 20));
aBall.color = Color.Red;
lock (this)
{
ballList.Add(aBall);
textBox1.AppendText("Read"+DateTime .Now .ToLongTimeString ()+ "\n");
}
this.Invalidate();
}
//產生紅色球球
bool flg1, flg2,flg3;
private void button1_Click(object sender, EventArgs e)
{
flg1 = true;
Thread th1 = new Thread(readtst);
th1.Start();
}
//執行緒目標
public void readtst()
{
while (flg1 == true)
{
textBox1.BeginInvoke(new creatball(rdball ));
Thread.Sleep(10);
}
}
//委派目標 (執行緒委派目標值傳回到form的物件)
private delegate void creatball( );
private void tst()
{
textBox1.AppendText("read ball" +DateTime .Now .ToLongTimeString ()+ "\n");
}
//委派目標 - 產生紅色球球
public void rdball()
{
Ball aBall;
aBall.pt = new Point(rd.Next(20, this.ClientSize.Width - 20), rd.Next(40, this.ClientSize.Height - 20));
aBall.color = Color.Red;
lock (this)
{
ballList.Add(aBall);
textBox1.AppendText("Read"+DateTime .Now .ToLongTimeString ()+ "\n");
}
this.Invalidate();
}
[c#] 由函式反向傳遞直給飲用者 應用介紹-[public void mystr(out string x)]
由函式反向傳遞值給引用者 應用介紹-[public void mystr(out string x)]
//函式引用者取值
private void button1_Click(object sender, EventArgs e)
{
string x;
mystr(out x);
textBox1.Text = x;
}
//反向送出函式
public void mystr(out string x)
{
x = "read";
Console.WriteLine(x);
}
//函式引用者取值
private void button1_Click(object sender, EventArgs e)
{
string x;
mystr(out x);
textBox1.Text = x;
}
//反向送出函式
public void mystr(out string x)
{
x = "read";
Console.WriteLine(x);
}
2009年12月26日 星期六
[c#] delegate and winform object 應用介紹-[delegate]
delegate and winform object 應用介紹-[delegate]
//如何使用delegate把對象傳回到windows form
//傳回到windows form 物件
private void button1_Click(object sender, EventArgs e)
{
textBox1.BeginInvoke(new creatball(tst));
}
//委派對象
public delegate void creatball();
public void tst()
{
textBox1.AppendText("aaaa");
}
//如何使用delegate把對象傳回到windows form
//傳回到windows form 物件
private void button1_Click(object sender, EventArgs e)
{
textBox1.BeginInvoke(new creatball(tst));
}
//委派對象
public delegate void creatball();
public void tst()
{
textBox1.AppendText("aaaa");
}
2009年9月6日 星期日
[c#] interface 應用介紹-[interface]
interface 應用介紹-[interface]
1.) interface 語法:
using System;using System.Collections.Generic;
using System.Text;
namespace areaFace
{
interface AreaInterface
{
void triangle(int x,int y); //三角形
int circle(int x, int y); //圓形 ,帶回傳值
void square(int x, int y); //方形
}
}
2.) interface實做:
using System;
using System.Collections.Generic;using System.Text;
namespace areaFace
{
public class AreaClass:AreaInterface
{
//圓circle面積公式: 圓面積=半徑×半徑×3.14
//正方形square面積公式: 長x寬=正方
//梯形面積: 上底*高/2 +下底*高/2=(上底+下底)*高/2
//三角形triangle面積: 底×高÷2
//圓面積
private int circlradius1;
private int circlradius2;
public int circlarea;
//圓面積建構子
public AreaClass(int circlradius1, int circlradius2)
{
//呼叫圓面積涵式
circlarea = circle(circlradius1, circlradius2);
}
//圓面積實做
public int circle(int x ,int y)
{
//圓circle面積公式: 圓面積=半徑×半徑×3.14
int circlearea;
circlarea = x * y;
Console.WriteLine(circlarea); //打印
return circlarea; //回傳
}
public void square(int x, int y)
{
//未完
}
public void triangle(int x, int y)
{
//未完
}
}
}
3.) 實做測試:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;using System.Drawing;using System.Text;
using System.Windows.Forms;
namespace areaFace
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
AreaClass ar = new AreaClass(10, 20);
MessageBox.Show(ar.circlarea.ToString () );
}
}
}
1.) interface 語法:
using System;using System.Collections.Generic;
using System.Text;
namespace areaFace
{
interface AreaInterface
{
void triangle(int x,int y); //三角形
int circle(int x, int y); //圓形 ,帶回傳值
void square(int x, int y); //方形
}
}
2.) interface實做:
using System;
using System.Collections.Generic;using System.Text;
namespace areaFace
{
public class AreaClass:AreaInterface
{
//圓circle面積公式: 圓面積=半徑×半徑×3.14
//正方形square面積公式: 長x寬=正方
//梯形面積: 上底*高/2 +下底*高/2=(上底+下底)*高/2
//三角形triangle面積: 底×高÷2
//圓面積
private int circlradius1;
private int circlradius2;
public int circlarea;
//圓面積建構子
public AreaClass(int circlradius1, int circlradius2)
{
//呼叫圓面積涵式
circlarea = circle(circlradius1, circlradius2);
}
//圓面積實做
public int circle(int x ,int y)
{
//圓circle面積公式: 圓面積=半徑×半徑×3.14
int circlearea;
circlarea = x * y;
Console.WriteLine(circlarea); //打印
return circlarea; //回傳
}
public void square(int x, int y)
{
//未完
}
public void triangle(int x, int y)
{
//未完
}
}
}
3.) 實做測試:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;using System.Drawing;using System.Text;
using System.Windows.Forms;
namespace areaFace
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
AreaClass ar = new AreaClass(10, 20);
MessageBox.Show(ar.circlarea.ToString () );
}
}
}
訂閱:
文章 (Atom)