Posts

Showing posts from November, 2010

Basic exception usage in php

Theory If you have not already, check the official php documentation on exceptions at http://php.net/manual/en/language.exceptions.php. Besides some php specifics, here you can find basic example how to use try catch blocks and how to work with exceptions. Example goes like this: function inverse($x) { if (!$x) { throw new Exception('Division by zero.'); } else return 1/$x; } try { echo inverse(5) . "\n"; echo inverse(0) . "\n"; } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; } // Continue execution echo 'Hello World'; Through this example you can see that if a process finds itself in a problematic situation, it can throw an exception. This exception can be caught, and that way we will prevent system to run into some php fatal error. This are the theoretical basics, however, the real question is how and where to use exception handling in your  applications and scripts. Front Controller Front C

Convert XmlDocument to XElement

XmlElement x = objXmlDocument.documentElement; You might need this if you are creating a WCF service that accepts XElement as parameter but user has a dataset to be converted to Xelement XmlDocument xDoc = new XmlDataDocument(YourDataset); XmlElement x = xDoc.DocumentElement; example :  DataSet ds = new DataSet();  ds.Tables.Add("students"); ds.Tables[0].Columns.Add("Name");  DataRow dr1 = ds.Tables[0].NewRow(); dr1[0] = "Alan"; ds.Tables[0].Rows.Add(dr1);  XmlDocument xDoc = new XmlDataDocument(ds);  XmlElement x = xDoc.DocumentElement;

NOKIA N900 upgrade

There are 3 ways to upgrade your N900. OTA Over The Air. YES! Upgrade your phone online. Its easily but might cause problems after OTA. PC suite Use NOKIA OVI suite or NOKIA Software Updater. Upgrade your phone with PC suite and backup your file before you starting. That's the safest way to upgrade your N900! But all chinese support will be lost after upgraded. Rewrite with flasher Get the flasher with latest ROM file. Rewrite the ROM with latest version. You don't have to rewrite eMMC content unless the system crashed. Besides, if you've been upgrade your phone but ran into some problems, you'd better rewrite your ROM again with the same version. All chinese support also will be lost after upgraded. You can easily done with above way for upgrading. Enjoy yourself! ps. I already upgraded my N900 to PR 1.3. XD

Why exception handling is important in php?

    I work as a professional programmer for 10 years and in those 10 years I met a bunch of different developers. In plenty of them, especially in web / php developers, I could notice a lack of understanding of the concept of throwing and catching exceptions and what does it serves for. Of course, the first I noticed it at myself. In this blog I would like to point out a number of useful things that proper use of the exception can make, and which were slipping from my hands for a few years too.      Exceptions are providing a standardized way to control problematic processes and they are reducing the possibility of fatal errors in the application . If process throws exception, you are forced to handle situation in your application, and it will automatically reduce the possibility that some problem sleeps out whit out being processed. When you're working with exceptions, you are handling problems at low level, so it will not make you problems with your higher layers, e.g displaying

Chinese font can not be show correctly with JAVA application on FreeBSD

I ran into this problem as I installed the freemind. After searching with Google, the following steps can solve the problem. Create a directory named "fallback" in JRE path # cd /usr/local/jdk1.6.0/jre/lib/fonts # mkdir fallback Link a TrueType that I wanted to the new directory # cd fallback/ # ln -s /usr/local/lib/X11/fonts/TrueType/bsmi001p.ttf ./bsmi001p.ttf Restart the JAVA application After restarting, the JAVA application will show chinese font properly. ps. Many thanks for Tim's help.

Homebrew - Another package manager for Mac OS X

前陣子在 plurk 上看到 Rex 大提到 Homebrew,一開始也不曉得那是什麼? 慢慢才知道原來是另一種套件管理工具。 今天在看到 ihower 大大的 blog 文章後,索性就動起來手來,先把 MacPorts uninstall 掉,然後再依照 ihower 大大提供的步驟把 homebrew 裝了起來。(作法請參考 ihower 大大的 原文 ) Homebrew 是一套 ruby based 的套件管理程式,它的特性如下﹕ 不佔用系統空間 不重覆安裝 mac 系統中已內建的工具 ruby based 的 portfile,易於修改、自訂 使用者可很快的建立自己想要的套件 個人用了之後,覺得 MacPorts 更新較快、內容較多,但,Homebrew 則是標榜不重覆,簡易使用。 果真簡易!

MacPorts uninstall

Following the steps below: Uninstall ALL installed packages $ sudo port -f uninstall installed Remove related directories $ sudo rm -rf \ /opt/local \ /Applications/DarwinPorts \ /Applications/MacPorts \ /Library/LaunchDaemons/org.macports.* \ /Library/Receipts/DarwinPorts*.pkg \ /Library/Receipts/MacPorts*.pkg \ /Library/StartupItems/DarwinPortsStartup \ /Library/Tcl/darwinports1.0 \ /Library/Tcl/macports1.0 \ ~/.macports Now, its clear and all installed packages from MacPorts was disappeared.

CU: Serial terminal emulator

cu,個人覺得是一個簡單、方便的 serial console 連線工具。 第一次看到是在 debian 上,透過 apt-cache search 找到的工具,裝好就可以用了。後來,在 OpenBSD 上就沒找到 cu。一度以為沒有這個工具,也試著想找它的 source code 來在 OpenBSD 上自編譯、使用未果。 這幾天在試玩新的 OpenBSD 4.8 時,才發現原來早就內建在系統中,我真是後知後覺啊!! orz $ sudo cu -l /dev/cuaU0 -s [speed] 超方便,但功能不會陽春哩!

Rules of screenrc

$ vim .screenrc hardstatus on hardstatus alwayslastline hardstatus string "%{.bW}%-w%{.rY}%n %t%{-}%+w %=%{..G} %H(%l) %{..Y} %Y/%m/%d %c:%s " bind b encoding big5 utf8 bind u encoding utf8 utf8