วันพฤหัสบดีที่ 10 กรกฎาคม พ.ศ. 2551

ลำดับการเรียก Exception ใน C#

การเกิด Exception ใน C# กรณีที่เป็น Nested ให้พิจารณาตัวอย่างต่อไปนี้


using System;
using System.Text;

namespace DemoEsception
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Point A");
try
{
Console.WriteLine("Point B");
// Try to convert wrong format
Console.WriteLine(int.Parse("abc"));
Console.WriteLine("Point C");
}
catch (FormatException e)
{
Console.WriteLine("Catch exception:{0}",e);
}
Console.WriteLine("Point D");
}
catch (Exception e)
{
Console.WriteLine("Outer exception:{0}",e);
}
Console.WriteLine("Point E");
Console.ReadKey();
}
}
}


ผลลัพธ์ที่ได้
Point A
Point B

Catch exception:System.FormatException: Input string was not in a correct format. at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at System.Int32.Parse(String s) at DemoEsception.Program.Main(String[] args) in D:\Data\Visual Studio 2008\Projects\DemoEsception\DemoEsception\Program.cs:line 17
Point D
Point E


สังเกตว่าใน code นี้เกิดความผิดพลาดขึ้นเมื่อเกิดความพยายามเปลี่ยนตัวอักษร abc ให้เป็นตัวเลขโดยใช้คำสั่ง int.Parse("abc") ซึ่งจะทำให้เกิด FormatException ขึ้น และเมื่อเกิด Exception ขึ้นนั้นโปรแกรมจะข้าม Point C แล้วทำงานในส่วนของ exception แล้วกลับไปทำงานยัง Point D และ Point E จะเห็นว่าโปรแกรมจะไม่เข้าไปดำเนินการ exception ด้านนอกเนื่องจากได้ดำเนินการ FormatException ไปเรียบร้อยแล้ว

พิจาณา Code ต่อไปนี้เพิ่มเติม

using System;
using System.Text;

namespace DemoEsception
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Point A");
try
{
Console.WriteLine("Point B");
// Try to convert wrong format
Console.WriteLine(int.Parse("abc"));
Console.WriteLine("Point C");
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine("Catch exception:{0}",e);
}
Console.WriteLine("Point D");
}
catch (Exception e)
{
Console.WriteLine("Outer exception:{0}",e);
}
Console.WriteLine("Point E");
Console.ReadKey();
}
}
}

ผลลัพธ์ที่ได้
Point A
Point B
Outer exception:System.FormatException: Input string was not in a correct format. at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at System.Int32.Parse(String s) at DemoEsception.Program.Main(String[] args) in D:\Data\Visual Studio 2008\Projects\DemoEsception\DemoEsception\Program.cs:line 17
Point E

จากตัวอย่างข้างต้นได้เปลี่ยนแปลง Exception เป็น IndexOutOfRangeException ซึ่งทำให้เกิด exception ที่ไม่ได้ดักจับไว้ ผลคือ โปรแกรมจะข้ามการดำเนินการ exception ภายในไปยัง exception ภายนอกซึ่งไม่ได้กำหนดชนิดของ exception ไว้จึงทำให้ การดำเนินการใน Point C และ Point D ถูกข้ามไป เมื่อทำงานในส่วน exception เสร็จแล้วโปรแกรมจึงกลับมาดำเนินการใน Point E ต่อไป

วันเสาร์ที่ 28 มิถุนายน พ.ศ. 2551

วิธีการสร้างแผ่น boot xp ให้สามารถทำงานกับ SATA (เฉพาะของ intel)

เครื่องมือที่ต้องมี
1. แผ่น CD Windows XP ต้นฉบับ
2. image สำหรับ boot xp
3. driver sata 2 ชุดด้วยกัน คือ iastor-sata-drivers.zip และ iastor-sata-i386.zip
4. โปรแกรม Nero

การทำงานจะแบ่งเป็น 2 ขั้นตอนคือ ขั้นตอนการเตรียมการ และ ขั้นตอนการไรท์แผ่น

ขั้นตอนการเตรียมการ
1. สร้าง folder ไว้สำหรับทำงานให้ภายในมี folder ย่อย 2 รายการ โดยในที่นี้จะให้ชื่อ folder ว่า OS ภายในมี folder ย่อยคือ boot และ root ดังนี้

d:\os
d:\os\boot
d:\os\root

2. copy ไฟล์จาก cd ต้นฉบับไปไว้ใน folder ที่ชื่อ root (ก่อน copy ไฟล์ให้เปลี่ยน option ของ folder option ให้เห็นไฟล์ทั้งหมดก่อนโดยเลือก tools->folder options->View แล้วเลือก Show hidden files and folders และ เอาติ๊กถูกหน้า Hide protected operation system files ออก)

3. สร้าง folder d:\os\root\$OEM$\$1\DRIVERS แล้วแตกไฟล์ iastor-sata-drivers.zip ลงใน folder ที่เพิ่งสร้าง

4. แตกไฟล์ iastor-sata-i386.zip ลงใน folder d:\os\root\i386

5. สร้างไฟล์ d:\os\root\i386\winnt.sif โดยเพิ่มข้อความนี้ลงไป

[Data]
AutoPartition=0
MsDosInitiated="0"
UnattendedInstall="Yes"

[Unattended]
UnattendMode=FullUnattended
UnattendSwitch=Yes
OemSkipEula=Yes
OemPreinstall=Yes
ConfirmHardware=No
OemSkipEula=Yes
TargetPath=\WINDOWS
Repartition=No
WaitForReboot="No"
DriverSigningPolicy=Ignore
OemPnPDriversPath="DRIVERS\SATA\IASTOR"

[GuiUnattended]
OEMSkipWelcome = 1

[UserData]
ProductKey=xxxxx-xxxxx-xxxxx-xxxxx-xxxxx
ComputerName=WorkStation
FullName=""
OrgName=""
หมายเหตุ ข้อมูลในส่วน UserData นั้นให้แก้ไขเองตามสะดวกนะครับ
- ProductKey คือ Keyของ windows
- ComputerName คือ ชื่อเครื่องคอมที่เราต้องการกำหนด
- FullName คือ ชื่อเจ้าของเครื่อง
- OrgName คือ ชื่อขององค์กร


6. เพิ่มข้อมูลลงในไฟล์ d:\os\root\i386\TXTSETUP.SIF ตาม Section ต่อไปนี้ครับ

- [SourceDisksFiles] เพิ่มข้อมูลนี้
iastor.cat = 1,,,,,,,1,0,0
iastor.inf = 1,,,,,,,1,0,0
iastor.sys = 1,,,,,,4_,4,1,,,1,4
iastor.sys = 1,,,,,,,1,0,0
iaahci.cat = 1,,,,,,,1,0,0
iaahci.inf = 1,,,,,,,1,0,0
-[HardwareIdsDatabase] เพิ่มข้อมูลนี้
PCI\VEN_8086&DEV_2922&CC_0106 = "iastor"
PCI\VEN_8086&DEV_282A&CC_0104 = "iastor"
PCI\VEN_8086&DEV_2829&CC_0106 = "iastor"
PCI\VEN_8086&DEV_2822&CC_0104 = "iastor"
PCI\VEN_8086&DEV_2821&CC_0106 = "iastor"
PCI\VEN_8086&DEV_2682&CC_0104 = "iastor"
PCI\VEN_8086&DEV_2681&CC_0106 = "iastor"
PCI\VEN_8086&DEV_27C3&CC_0104 = "iastor"
PCI\VEN_8086&DEV_27C6&CC_0104 = "iastor"
PCI\VEN_8086&DEV_27C1&CC_0106 = "iastor"
PCI\VEN_8086&DEV_27C5&CC_0106 = "iastor"
PCI\VEN_8086&DEV_2653&CC_0106 = "iastor"
-[SCSI.Load] เพิ่มข้อมูลนี้
iastor = iastor.sys,4
-[FileFlags] เพิ่มข้อมูลนี้
iastor.sys = 16
-[SCSI] เพิ่มข้อมูลนี้
iastor = "Intel(R) SATA RAID/AHCI Controller"
7. เพิ่มข้อมูลลงในไฟล์ d:\os\root\i386\DOSNET.INF ตาม Section ต่อไปนี้ครับ

-[FloppyFiles.1] เพิ่มข้อมูลนี้
d1,iastor.sys
-[Files] เพิ่มข้อมูลนี้
d1,iastor.cat
d1,iastor.inf
d1,iastor.sys
d1,iaahci.cat
d1,iaahci.inf
ถึงตรงนี้ถือว่าขั้นตอนการเตรียมการเสร็จสิ้นแล้ว ต่อไปจะเป็นการ write แผ่น cd โดยใช้โปรแกรม Nero

การไรท์แผ่น boot windows xp

1. แตกไฟล์ image สำหรับ boot xp ลงใน folder d:\os\boot\ ที่ได้เตรียมไว้

2. เปิดโปรแกรม nero โดยเลือกหัวข้อ cd-rom(boot)

3. กำหนดตัวแปรดังนี้
บูต--> อิมเมจไฟล์ [d:\os\boot\boot.ima]
-> ขั้นสูง เปิดใช้การตั้งค่าของผู้เชี่ยวชาญ (สำหรับผู้ใช้ระดับสูงเท่านั้น) [ติ๊กถูก]
-> ลักษณะของอิมูลเลชั่น [ไม่มีอีมูเลชั่น]
-> โหลดเซ็คเมนต์เซ็คเตอร์(hex!) [0000]
-> จำนวนเซ็คเตอร์ที่โหลด [4]

ISO--> โหมดข้อมูล [โหมด 1]
-> ระบบไฟล์ [ISO 9660 + โจเลียต]
-> ชื่อไฟล์ความยาม(ISO) [สูงสุดของ 31 ตัวอักษร (level 2)
-> ชุดอักขระ (ISO) [ISO 9660 (สแตนดาร์ด ISO CD-รอม)]

(ติ๊กถูกทั้งหมดนี้)
-> อนุญาตความลึกของพาธมากกว่า 8 ไดเรกทอรี่
-> อนุญาตให้ใช้ตัวอักในในพาธมากกว่า 255 ตัว
-> ห้ามใส่นามสกุึลเวอร์ชั่นไฟล์ ISO '1'
-> อนุญาตให้มากกว่า 64 ตัวอักษรสำหรับชื่อโจเลียต
เลเบล-->