Create and raise custom errors

Hello,

I have been trying to implement Python Exceptions in order to control the flow of the dynamo code and inform the user (in case of using the file in Dynamo or in Dynamo Player) so they know what the problem is. It could sound a bit odd but sometimes the input have to accomplish some conditions (as in levels to have different names, if not revit is going to crash).

I have already create a python code that raise an exception with a message that shows only in dynamo workspace and stop the processing returning a null object. Nevertheless, if i add-in this code inside a custom node the error is invissible and no message is seen. I would like to achieve the full view of this message anyway, in the custom code box and inside the Dynamo Player space.

This is the code i used inside my custom node, but it only shows inside of it, not in the general sheet.

Thank you very much

Hi @Alvaca and welcome

maybe you can create your custom Exception class with a message box

an example

import sys
import clr
import System
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference('System.Windows.Forms')
from System.Windows.Forms import MessageBox, MessageBoxButtons, MessageBoxIcon

class ValidationError(Exception):
	def __init__(self, typeError = "Error", messageError = "An error occurred"):
		self._typeError = typeError
		self._messageError = messageError
		Exception.__init__(self, self._typeError)
		self.CMessageBox()
		
	def __str__(self):
		return "{} : {}".format(self._typeError , self._messageError)
		
	def CMessageBox(self):
		MessageBox.Show(self._messageError, self._typeError, MessageBoxButtons.OK, MessageBoxIcon.Error)
		
		
pt = IN	[0]
if pt.Z > 3:
	raise ValidationError("Point Error", "Z value is too high")
OUT = 0
3 Likes

Thank you very much @c.poupin. It works as I wanted to, it is perfect.
I just changed the code so it fits with my code necessities and everything go as it has to.

I add my code in case anyone is interested in my specific case:

# Carga las bibliotecas de DesignScript y normas de Python.
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference('System.Windows.Forms')
from System.Windows.Forms import MessageBox, MessageBoxButtons, MessageBoxIcon

# Las entradas de este nodo se almacenan como lista en las variables IN.
dataEnteringNode = IN
dataInput = IN[0]
isDuplicated = IN[1]

# Incluya el código debajo de esta línea

class ValidationError(Exception):
	def __init__(self, typeError = "Error", messageError = "An error occurred"):
		self._typeError = typeError
		self._messageError = messageError
		Exception.__init__(self, self._typeError)
		self.CMessageBox()
		
	def __str__(self):
		return "{} : {}".format(self._typeError , self._messageError)
		
	def CMessageBox(self):
		MessageBox.Show(self._messageError, self._typeError, MessageBoxButtons.OK, MessageBoxIcon.Error)	

if isDuplicated:
	raise ValidationError("Input Error", "Los nombres y cotas de los niveles deben ser diferentes")
else:
	OUT = dataInput