Tuesday, October 21, 2008

.net try catch finally

Yesterday, dechu said he once read my blog. I then realize I have a blog... Almost forgot this place. Today I am going to talk about try, catch, finally.
Few years ago, I used try catch to avoid unexcepted error, and always put everything in a big try block. Catch the error, maybe print it out and that's it. This is a really bad practice.
I started to look at other people's code, and learned to do some clean up in finally block, however, this is still not enough. Now I started to use multiple level try & catch, in the lower level,
trying to rollover previous actions like a sql transaction in catch block, and then throw new exception to upper level code. This improve the error handling abilities and allow code structure to be easily handled. The disadvantage is the performance issue of throwing exceptions.
But considering we are not using exceptions mechanism to achieve certain result, the time consuming exception won't eat up resources in the normal run.

First look at this code below:


/ try-catch-finally
using System;public class TRYCATCHFINALLY
{
public static void Main ()
{
try
{
Console.WriteLine("Executing the try statement.");
try
{
DoSomething();
}
catch (System.Exception sex)
{
throw new SomethingFailedException();
}
Console.WriteLine("Now something done, try other things now.");
try
{
DoOtherthing();
}
catch (System.Exception oex)
{
throw new OtherthingFailedException();
}
Console.WriteLine("Executing the try all done.");
}
catch (SomethingFailedException sfe)
{
Console.WriteLine("{0} Caught exception #1.", e);
}
catch (OtherthingFailedException ofe)
{
Console.WriteLine("{0} Caught exception #1.", e);
}
finally
{
Console.WriteLine("Executing finally block. Clean up
everything.");
}
}
private void DoSomething()
{
try
{
//Hello 6sb
}
catch (System.Exception e)
{
//Rollover
throw new Exception WhateverException();
}
finally
{
// clean up
}
}

private void DoOtherthing()
{
try
{
//lv 6sb
}
catch (System.Exception e)
{
//Rollover
throw New Exception WhateverException();
}
finally
{
// clean up
}
}
}



Wednesday, October 8, 2008

.Net 的优缺点

.Net === 一堆库+虚拟机
.Net + C# === Java + JVM
普遍觉得有以下优点:
1. MS主推
2. 支持多语言
3. .NET的核心虚拟机CLR将c#这种编译出来的CIL中间代码再编译成机器代码,而非jvm的解析,所以运行速度快
4. 安全,内存,异常处理都在虚拟机内得到一定的保证。
5. 跨平台ms没做,支持了一个叫Mono的项目,可以在Linux, Solaris, Mac OS X, BSD, HP-UX下面跑,不过现在也就做到.Net 2.0
 
我自己认为的好处:
1. 写完程序配置起来简单,基本上机器都会在自动跟新的时候装上这个.net runtime envrionment
2. 学起来容易,不管是web application还是desktop software,学了很大一部分能通用
3. VS用起来蛮舒服。
4. 跟sqlserver也结合紧密,甚至可以说跟ms的所有东西都结合紧密。
5. 开速开发GUI的首选吧。
6. 开发web service嗷嗷简单,客户端如果也用.net,API这块省力。
 
缺点:
1. 代码保护不容易,跟java似的。。
2. 更新太快,丫的3.0还好,3.5就是一个更新的平台,引进一堆没听过的东西。。现在已经出4.0了。
3. 跟人说会这么好像很没面子,不如gs的python来的野性。。。哈哈
4. 有些功能没有,要调用windows library里面的函数了,MFC能干的丫的managed code不一定能干,只能走unsafe code的路子,但是前面的优势就没那么明显了。

 

Monday, September 29, 2008

SQL Server Profiler fonts error

运行SQL Server Profiler,生成,连接数据库,正常,当点下run开始跟踪的时候,报错:"only TrueType fonts are supported. There id not a TrueType font"。倒,微软还真进步,看来要淘汰windows中的非TrueType字体了。

避免该错误,需要修改Profiler的指定字体,方法如下:

点击 Tools菜单->Options...

然后在选项对话框开头的Display Options部分,"Choose Font..."换一种TrueType的字体即可,我比较喜欢Vendana 。

Monday, September 8, 2008

windows form c# -- User Interface tips to show system status

When a time consuming task is performed, we can either perform it using backgroundworker, or using a little tips like this:
Cursor.Current = Cursors.WaitCursor;
// Perform your task here
Cursor.Current = Cursors.Default;
From the UI perspective, User understand that the system is busy and will be more than happy to wait rather than disappointed.

Saturday, September 6, 2008

General Linux Common Command

Generic Linux

COMMAND NAME		DESCRIPTION
df -h shows disk usage in human readable form
man <commandname> shows more info about a command
uname -a kernel release version
/usr/sbin/smbd -V samba version
/usr/sbin/httpd -v apache version
mysql -v mysql version
php -v php version
mv moves or renames a file
cp copies or backsup a file
rm removes or deletes a file
ps -ax|grep <process> outputs processes running <process>
ps -AH report process status
top shows processes
top -i shows only active processes
iptraf shows network info
mc -d show midnight commander to navigate through system easily
cat /proc/mdstat shows software raid
host -t mx aol.com shows the mx records for aol.com
net groupmap list shows samba mappings to nt groups
telinit 1 changes to single user mode
ifconfig shows detailed info on ethernet ports
grep -r "casesensitivesearch" * finds all documents containing the criteria in a dir
tail -f /var/log/<LOGFILE> realtime viewing of your log file
hdparm -t /dev/mdx (where x is 0,1,2,etc) shows software raid performance
mdadm --detail /dev/mdx (where x is 0,1,2,etc) gives raid info
tar -czvf foo.tar.gz foo creates a tar/zip file of a directory
tar -xvzf foo.tar.gz untar/unzip a tar/zip file
scp -P <ssh_portnumber> foo.tar.gz <other_server_ipaddress>:/opt transfers file to another server
rsync --progress -te "ssh -p <ssh_portnumber>" foo <other_server_ipaddress>:/opt transfers file to another server
sed -i s/foo/fee/g <FILENAMEORPATHTODIR> replaces foo with fee