User Tools

Site Tools


script:walkthrough:exceptions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
script:walkthrough:exceptions [2019-11-20 20:54]
skyjake [Try and catch]
script:walkthrough:exceptions [2019-11-20 21:29]
skyjake [Catching native errors]
Line 5: Line 5:
 ===== Try and catch ===== ===== Try and catch =====
  
-Like in C++, the ''​try''​ and ''​catch''​ keywords are used to begin compounds ​of code where exceptions can be thrown and processed.+Like in C++, the ''​try''​ and ''​catch''​ keywords are used to begin blocks ​of code where exceptions can be thrown and processed.
  
 If an exception is thrown outside ''​try''/''​catch'',​ it will be  uncaught and execution of the script to be aborted. If an exception is thrown outside ''​try''/''​catch'',​ it will be  uncaught and execution of the script to be aborted.
Line 11: Line 11:
   try: print '​Nothing thrown yet.'   try: print '​Nothing thrown yet.'
   catch: print '​Skipped.'​   catch: print '​Skipped.'​
 +
 +===== Throwing exceptions =====
  
 Exceptions are thrown using a ''​throw''​ statement. Exceptions are thrown using a ''​throw''​ statement.
Line 22: Line 24:
   end   end
  
-If the exception does not require any handling, one can use ''​pass''​ as the catch compound.+===== Handling exceptions ===== 
 + 
 +If the exception does not require any handling, one can use ''​pass''​ as the catch block.
  
   try: throw '​Problem!'​   try: throw '​Problem!'​
   catch: pass   catch: pass
  
-The exception message can be assigned to a local variable so it can be accessed in the catch compound.+The exception message can be assigned to a local variable so it can be accessed in the catch block.
  
   try: throw "​OMG!"​   try: throw "​OMG!"​
Line 38: Line 42:
   Caught: [Error] (in script) OMG!   Caught: [Error] (in script) OMG!
  
-Try/catch can be nested, and an exception thrown in an inner try compound ​can be caught in an outer compound.+ 
 +Multiple ''​catch''​ blocks can be chained together to catch specific types of exceptions.  
 + 
 +  try 
 +      throw 'This will be printed'​ 
 +       
 +  catch NullError: print 'Got NullError'​ 
 +  catch Error, er: print er 
 +  catch: print 'Never here.'​ 
 + 
 +Try/catch can be nested, and an exception thrown in an inner try block can be caught in an outer block.
  
   try   try
Line 56: Line 70:
   end   end
  
-In this example, BogusError does not match the type of error thrown in scripts (a generic Error), so the catch compounds ​are skipped. The output is:+In this example, BogusError does not match the type of error thrown in scripts (a generic Error), so the catch blocks ​are skipped. The output is:
  
   Got it: [Error] (in script) Deep.   Got it: [Error] (in script) Deep.
  
-  *  Throwing ​exception in a function+An exception ​thrown ​in a function ​unwinds the call stack to find matching catch block.
-  *  Catching a specific type of exception. +
-  *  Multiple catch statements. +
-  *  Exception thrown in another module. +
-  *  Exception thrown in recursive ​call+
-  *  Exception from trying ​to change ​read-only variable.+
  
 +  def erroneous()
 +      throw "​Dropped the ball"
 +  end
 +  ​
 +  try
 +      print "​Calling erroneous()"​
 +      erroneous()
 +      try
 +          print 'This will be skipped!'​
 +      catch
 +          print 'I am on the wrong catch level.'​
 +      end
 +  catch
 +      print '​Fumbled it...'
 +  end
  
 +===== Catching native errors =====
  
 +Native code may throw various kinds of exceptions that can then be caught in the script. In the example below, a ''​NullError''​ gets thrown because the code tries to access a deleted object via an alias.
 +
 +  try
 +      record temp
 +      reference = temp
 +      del temp
 +      print reference
 +  catch NullError, er
 +      print 'Oh noes:',​ er
 +  end
 +
 +''​ReadOnlyError''​ is thrown when trying to change a ''​const''​ variable.
 +
 +  const SOME_CONSTANT = 3.1415
 +  try
 +      SOME_CONSTANT = 3
 +  catch ReadOnlyError,​ er
 +      print '​Cannot do it.', er
 +  end
 +  try
 +      const SOME_CONSTANT = 3
 +  catch ReadOnlyError,​ er
 +      print 'Will not work either:',​ er
 +  end
  
 +Any type of native exception can be caught in a script.
script/walkthrough/exceptions.txt · Last modified: 2019-11-20 21:29 by skyjake