except ( Parametric )

Also see :


Examples :

The MultiMemberLocate() function for selecting multiple members throws a SystemError exception when the user right-clicks and chooses " Cancel " instead of selecting members. No extra import is needed to catch this exception since SystemError is a Python error. The following script catches the exception and prints to the report viewer ( Output-Request Summary ) the message " You canceled without selecting any members ":

# Prints piecemark [num] of each member in the selection.
from member import MultiMemberLocate
from param import ClearSelection
try:
   mem_list = MultiMemberLocate("Select some members")
   print("You selected the following members:")
   for each_mem in mem_list:
      print(each_mem.Piecemark, "[",each_mem.MemberNumber,"]")
except SystemError:
      print("You canceled without selecting any members!")
ClearSelection()

When the following script is Run and the user presses " OK " on the floating point prompt dialog, the value entered on that dialog is printed in millimeters and in inches. If the user presses " Cancel " on the prompt dialog, pass ends the running of script. Note that ResponseNotOK needs to be imported in order for you to catch this exception. Also note that since the prompt is a floating point prompt, not a dimension prompt, there is no need for startup code Units() .

# Converts a distance entered in mm to inches.
from param import Prompt, ResponseNotOK
try:
   mm = Prompt(100.0, "Enter a number of millimeters:")
   inches = mm / 25.4
   print("The number of mm you entered =", mm)
   print("This number converted to inches =", inches)
except ResponseNotOK:
   pass

Tip: See Copying and Pasting scripts from Help for information on how to paste these scripts into blank .py text files that you can Run as parametrics in Modeling .


To catch an exception :

Following is a script that generates horizontal and vertical construction lines through any point the user locates. See Cutting and Pasting scripts from Help for information on how to paste this script into a file that you can Run in Modeling .

# Adds construction lines through a user-located point.
from point import PointLocate
import cons_line                #to catch the exception
from cons_line import ConsLine
try:
   cl1 = ConsLine()              # horizontal, green construction line
   cl1.Point1 = PointLocate("Locate a point")
   cl1.Rotation = 0
   cl1.PenNumber = "Green"
   cl1.Add()
   cl2 = ConsLine()               # vertical, white construction line
   cl2.Point1 = cl1.Point1
   cl2.Rotation = 90
   cl2.PenNumber = "White"
   cl2.Add()
except cons_line.error:
   print("You returned instead of locating a point!")

General guidelines for catching exceptions ( examples from above ):

Import the appropriate module ( import cons_line ) unless the exception is from the param module or unless it is a built-in Python exception. If the exception is from the param module, you need to import ResponseNotOk .

Beneath a try statement ( try: ), indent the code that generates the error.

An except statement ( except cons_line.error ) catches the exception, which in most cases is named after the module (examples: cons_line.error, member.error, bnt_plate.error ). For the param module, the exception is ResponseNotOK (see examples ).

Finally, indented beneath the except statement, you need to specify the error message you want ( print("You returned instead of locating a point!") ). Sometimes you may find that a better alternative to print is continue or pass . Or you can print the system-generated error message as shown in the example below.

Here's another script that shows these general guidelines in action:

# Member number needs to be invalid to get the exception.
from member import Member
import member                      #to catch the exception
try:
   mem1 = Member(1000)
   print(mem1.Piecemark)
except member.error, e:
   print("This is the error string:", e)