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
Last revision Both sides next revision
script:walkthrough:exceptions [2019-11-20 21:16]
skyjake [Try and catch]
script:walkthrough:exceptions [2019-11-20 21:19]
skyjake
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 21: Line 23:
       print '​Exception caught'​       print '​Exception caught'​
   end   end
 +
 +===== Handling exceptions =====
  
 If the exception does not require any handling, one can use ''​pass''​ as the catch block. If the exception does not require any handling, one can use ''​pass''​ as the catch block.
Line 37: Line 41:
  
   Caught: [Error] (in script) OMG!   Caught: [Error] (in script) OMG!
 +
 +
 +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/catch can be nested, and an exception thrown in an inner try block can be caught in an outer block.
Line 77: Line 91:
       print '​Fumbled it...'       print '​Fumbled it...'
   end   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. 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.
Line 88: Line 104:
       print 'Oh noes:',​ er       print 'Oh noes:',​ er
   end   end
- 
-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.' 
  
 ''​ReadOnlyError''​ is thrown when trying to change a ''​const''​ variable. ''​ReadOnlyError''​ is thrown when trying to change a ''​const''​ variable.
script/walkthrough/exceptions.txt ยท Last modified: 2019-11-20 21:29 by skyjake