Exception Handling: Difference between revisions
From Catglobe Wiki
More actions
| Line 3: | Line 3: | ||
__NOTOC__  | __NOTOC__  | ||
An exception is an error occurs in the runtime (the excution) of program. The CGScript language uses the '''try/catch statement''' and the '''throw''' expression to implement   | An exception is an error occurs in the runtime (the excution) of program. The CGScript language uses the '''try/catch statement''' and the '''throw''' expression to implement exception handling.  | ||
=== <span style="color:#DF8621;">'''Syntax'''</span>Β  ===  | === <span style="color:#DF8621;">'''Syntax'''</span>Β  ===  | ||
Revision as of 06:22, 20 December 2011
Exception Handling (Error Runtime Handling)
An exception is an error occurs in the runtime (the excution) of program. The CGScript language uses the try/catch statement and the throw expression to implement exception handling.
Syntax
try-catch statement
exception declaration     
// you must declare your exception variable before using it the try catch statement
// And the data type of this exception variable should be an object type (E.g. object e;) so that it can catch any kind of objects thrown by system or users
try {
   // codes that could throw an exception
}
catch (exception) {
   // codes that execute when exception is thrown in the try block
}
- In the try clause, when a line of code causes the exception/error, the exception will be thrown to the catch clause and the codes in catch clause will process that exception. All codes below the code which cause the exception/error will be ignored.
 - The exception can be any type (E.g. exception object, number, string...)
 - CGScript language does not support multi catch clauses.
 - Both try and catch clauses can contain nested try/catch statements.
 
throw expression
throw [expression]
- The expression can be any type (E.g. exception object, number, string...)
 - Used in try-catch statement only.
 - If throw is used in catch clause, it will be re-throw. See below examples for information about re-throw.
 
See also
- Exception class for more information about the error object of non-users (system) thrown errors.
 
Examples
Example 1:
object e;
string Name = "ManTu";
try {
      Age = 20;   // The Age variable is not declared, so this line will cause exception/error. An exception will be thrown to the catch clause
                  // And all codes below this line (Age = 20;) will not be ignored.
      print("Hello " + Name + "! Your Age is: " + Age);
} 
catch(e) {
    print("====There 's an error with your scriptΒ :)======");
    print(e.ToString());
}
======================================================
Example 2:
string e;
string Name = "ManTu";
number Age = 0;
try {
      if(Age == 0) 
         throw "Age can not be zero!";    // use throw expression in try clause to throw a specific exception to catch clause
      else
        print("Hello " + Name + "! Your Age is: " + Age);
} 
catch(e) {
    print(e);   // print out: Age can not be zero!
}
======================================================
Example 3: Re-throw
object e;
try
{
   try {
      s = 10;
   } 
   catch(e) {
     throw;  // throw in this case will be re-throw. It will re-throw the exception to the try-catch statement which contain it.
   }
}
catch(e) // this catch clause will receive the exception that re-thrown by above catch clause
{
   print("s is not declared yet.");
}