| **Navigation:**  [[clarion.htm|Clarion.Net (Clarion#)]] > [[clarion net language reference.htm|Clarion# Language Extensions]] > Exception Handling >====== TRY-CATCH-FINALLY (exception handling block) ====== | [[throw signal an exception .htm|{{btn_prev_n.gif|Previous page}}]][[clarion net language reference.htm|{{btn_home_n.gif|Return to chapter overview}}]][[runtime expression evaluation 1.htm|{{btn_next_n.gif|Next page}}]] | | || {{newcnet.jpg|NewCNet.jpg}} **TRY** //state////ments that can cause an exception// **CATCH** //state////ments// //to handle exception// **FINALLY** //state////ments// //to cleanup after an exception// **END** | **TRY** | Arm exception handling | | //statements// | any code that may produce an exception. | | **CATCH** | Trap an exception | | //statements// | Code to perform the appropriate recovery or alert the user. This code is only executed if an exception occurs. You may have multiple CATCH blocks. At the end of each CATCH block you can either throw the same exception (See [[throw signal an exception .htm|THROW]] statement), throw a different exception, or let the code fall out of the CATCH block. | | **FINALLY** | Clean up exception handling | | //statements// | Code in this block is always executed and can perform cleanup after an exception. The FINALLY block is optional. | {{blk2blue.jpg|blk2blue.jpg}} **TRY-CATCH-FINALLY** is a code block used for exception handling. If any exception occurs within the TRY block, the .Net runtime searches the CATCH blocks for an appropriate exception. If you have multiple CATCH blocks put the more specific exceptions at the top and the more generic exceptions last. The FINALLY block is always executed and can be used to do cleanup operations after an exception. The TRY block must be followed by a CATCH or FINALLY. You can have a TRY block without a CATCH as long as you have the FINALLY block. **Examples:** ** CODE ** ** ****TRY ** **  F()  !calling a function ** **  CATCH (Exception e)** **   MESSAGE(**'**Exception in code: **'** & e.Message) ** ** END** **!******************************** ** CODE** ** FOREACH ctrl in SELF.Controls** **  IF(ctrl Is TextBox)** **   IF(NOT ctrl.Tag&=null)** **    colnm&=ctrl.Tag** **    tab&=SELF.m_Row.Table** **    tp&=tab.Columns[colnm].DataType** **    val&=(ctrl TryAS TextBox).Text** **    IF((ctrl TryAs TextBox).Modified)** **     SELF.Modf=true** **     ****TRY** **      SELF.m_Row[colnm]:=Convert.ChangeType(val,tp)** **     ****CATCH**** Exception ex** **      MESSAGE('Invalid value in field: ' & colnm)** **      SELF.DialogResult=DialogResult.None** **      RETURN** **     ****END** **    END** **   END** **  END** ** END** ** CODE ** ** ****TRY ** **  F()  !calling a function ** ** ****CATCH**** (Exception e)** **   MESSAGE(**'**Exception in code: **'** & e.Message) ** ** ****FINALLY** **   !Any cleanup code here** ** END** **CheckErr    PROCEDURE** ** CODE** **  IF(ERRORCODE()<;>0)** **   ****THROW**** NEW DataException('ERROR: ' & ERROR())** **  END** **See Also:** [[throw signal an exception .htm|THROW]]