Pages

Subscribe:

Monday, January 9, 2012

How to handle exception in delphi

Almost in all programming language, there are mechanism to handle exception. So do in delphi. We can use try...except mechanism. The following is the example of exception handling in delphi :
var
  bagi, nol : Integer;
begin 
  try
    nol   := 0;
    bagi := 1 div zero;
    ShowMessage('1/0 = ' + IntToStr(bagi));
  except
    on E : Exception do
      ShowMessage(E.ClassName + ' error raised, with message : ' + E.Message);
  end;
end;
In that example, if the try clause generates an exception, it will the execute except clause that will display window showing what type of error generated. This is used to take alternative action when something unexpected goes wrong. Beside that, the other form of handle exception can be done as follow :
var
  bagi, nol : Integer;
begin 
  try
    nol   := 0;
    bagi := 1 div zero;
    ShowMessage('1/0 = ' + IntToStr(bagi));
  finally
    if bagi = -1 then
    begin
      ShowMessage('Number was not assigned a value - using default');
      bagi := 0;
    end;
  end;
end;
In a try...finally construct, the finally statement will be executed absolutely whether thereis error or not. It is normally used to allow cleanup processing, such as freeing memory etc. Both kind of construct cannot be combined together using try..except..finally. So, the question is how to handle exception while we want to execute a set of clean up statements, in other words, how to tricky combine both except and finally ?? This can be done with nested try statements, as construct below :
Try
  Try
    ...
  Except
    ...
  End;
Finally
  ...
End;
Need to be noticed that when you want to test exception handling, don't do it using F9/Run. But do it by execute application (ctrl+f9 then double click application.exe created)

Sunday, January 8, 2012

How to convert date to char in oracle

In every dbms, conversion is something that almost always happen. In oracle, if we want to convert date to char we can use to_char() function. otherwise, if we want to convert char to date we can use to_date() function.
Below is the sample to_char() and to_date() usage in oracle :
select to_char(sysdate, 'DD/MM/YYYY HH24:MI:SS') from dual;
select to_date('29/03/2012 08:18:45', 'DD/MM/YYYY HH24:MI:SS') from dual;
The first result will be the current date time of your database server
The second result will be '29/03/2012 08:18:45'.

Saturday, January 7, 2012

How to install pear modules in xampp

Below is the step to install pear in xampp :
  1. Search the path of executable pear
    usually it is in 'C:\xampp\php\pear.bat'
  2. Run the following command, for example we will install MDB2 module :
    pear install MDB2
    

Friday, January 6, 2012

How to insert post programmatically in wordpress

Sometimes, we need to insert post to our blog through code. This case occur in condition for example when we need to insert more than 1 post in 1 time.
Here is the code to do that :
  // Create post object
  $my_post = array();
  $my_post['post_title'] = 'My post';
  $my_post['post_content'] = 'This is my post.';
  $my_post['post_status'] = 'publish';
  $my_post['post_author'] = 1;
  $my_post['post_category'] = array(8,39);
  
  // Insert the post into the database
  wp_insert_post( $my_post );

Thursday, January 5, 2012

How to create xml document in PHP

Using domdocument, we can create xml document as we wish. Combined with the how-to-create-xml-in-php we will create xml directly in browser.
Here is the code to create xml from php :
$domDocument = new DOMDocument('1.0', "UTF-8");
  $domElement = $domDocument->createElement('field','some random data');
  $domAttribute = $domDocument->createAttribute('name');
  
  $domAttribute->value = 'attributevalue';
  
  $domElement->appendChild($domAttribute);

  $domDocument->appendChild($domElement);

  header("Content-type: text/xml");
  echo $domDocument->saveXML();