Rocket U2 | UniVerse & UniData

 View Only

 u2Python - best practive to raise @PYEXCEPTION... values

Manu Fernandes's profile image
PARTNER Manu Fernandes posted 03-25-2024 05:41

hello,

I want to raise errors in my py scripts then these error catch by into @PYEXCEPTIONTYPE , @PYEXCEPTIONMSG, @PYEXCEPTIONTRACEBACK 

What can be the best method ? 

a/ to re-raise a error from a try sequence :

these sample can catch 3 types of error : path not found, json not correct, mykey not found ... 

def myfunc(path):
    result = ''
    try:
        f_path = open(path)
        config = json.load(f_path)
        a = config["mykey"]       
    except:
        raise 

how can I have it at BASIC level into @PYEXCEPTIONTYPE , @PYEXCEPTIONMSG, @PYEXCEPTIONTRACEBACK  

in this case, @PYEXCEPTIONTYPE , @PYEXCEPTIONMSG, @PYEXCEPTIONTRACEBACK are null.

b/ to raise a error from script :

def myfnc(a)
     if a < 2:
          raise ValueError(f"integer above 1 expected, got {a}")

in this case, @PYEXCEPTIONTYPE , @PYEXCEPTIONMSG, @PYEXCEPTIONTRACEBACK are null.

Thanks

manu

John Jenkins's profile image
John Jenkins

Manu,

I did some digging - does this work for you?

def demo_catch():
    try:
        raise ValueError('This is ValueError')
        raise Exception('This is the exception')
    except Exception as error:
        print('Caught error: ' + repr(error))

>>> demo_catch()

Regards

JJ
Mike Rajkowski's profile image
ROCKETEER Mike Rajkowski

Manu,

I am wondering why you would expect anything when you raise an exception without defining what you raised, in the first example.

The 2nd code segment would raise the exceptions and pass @PYEXCEPTIONTYPE , @PYEXCEPTIONMSG,  and @PYEXCEPTIONTRACEBACK

forum.py

-----

def myfnc(a):
     if a < 2:
          raise ValueError(f"integer above 1 expected, got {a}")
     return a

------

TEST

----

ModuleName = "forum"
FuncName = "myfnc"
*            call the Python function
CRT "Enter data ":;INPUT stuff
IF NUM(stuff) THEN stuff += 0
pyresults = PyCallFunction(ModuleName, FuncName, stuff)
 
IF @PYEXCEPTIONTYPE = '' THEN
  CRT "OUTPUT = " :pyresults
END ELSE
  CRT "    EXCEPTION TYPE:" :@PYEXCEPTIONTYPE
  CRT " EXCEPTION MESSAGE:" :@PYEXCEPTIONMSG
  CRT "EXCEPTIONTRACEBACK:" :@PYEXCEPTIONTRACEBACK
END
 
END
-----
Then when I run, I get:
>RUN PBP TEST
Enter data ?0
    EXCEPTION TYPE:ValueError
 EXCEPTION MESSAGE:integer above 1 expected, got 0
EXCEPTIONTRACEBACK:Traceback (most recent call last):
 
  File "C:\U2\UV\XDEMO\PP\forum.py", line 4, in myfnc
    raise ValueError(f"integer above 1 expected, got {a}")
 
ValueError: integer above 1 expected, got 0
So, I am not sure why you mentioned you received null?
As for best practice, I would recommend you create your own custom exception, and raise that in your code.
Following is a good site that shows how this can be done:
https://www.programiz.com/python-programming/user-defined-exception#:~:text=In%20Python%2C%20we%20can%20define,pass%20try%3A%20...
Mike
Manu Fernandes's profile image
PARTNER Manu Fernandes

Hi Mike, 

In my sample, error can come from  : path not found, json not correct, mykey not found.

Try: jump to except at first error. 

If I place a 'raise' alone my script is supposed to re-raise the last error to the caller.

I do not want to handle all potentialité errors into the py module, the calller (uvbasic) will do it.

In my case, when except occurs uvbasic did not receive the @PYEXCEPTION, all are null.

(the sample you provide first works correctly).

Many thanks for your help. 

Manu