cancel
Showing results for 
Search instead for 
Did you mean: 

throw runtimeexception

Former Member
0 Kudos


I ws looking at some code written by a consultant we hired for a special project. In the main processing function, the entire function is bracketed with a try/catch. At certain points, he throws a runtimeexception if something programmatically occurs such as an invalid parameter is passed in or sqlca.sqlcode <> 0. In other cases, he throws a descendant of n_exception.

In the catch block, when the runtimeexception is caught, he logs the properties on the exception such as the line number and the object name. I'm wondering if these properties are even set correctly, i.e., are they set just by throwing the object, or are they set when the object is contructed. Mind you, the runtimeexception object is created once during the constructor, not just before it is thrown.

I was under the impression that the runtime engine threw these, not the program itself. Is this an accepted practice?

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Addendum: The runtimeexception is never instantiated. Comment in code says so. He just throws it so that the script line number is stored. Am I missing something? Bigger question is whether this is good code or not.

former_member190719
Active Contributor
0 Kudos

How about posting some of the code? 

With regard to this question:  "I'm wondering if these properties are even set correctly, i.e., are they set just by throwing the object, or are they set when the object is consructed."  the answer is in the description of the throw command in the PB help:

"When a RuntimeError, or a descendant of RuntimeError, is thrown, the instance variable containing line number information will be filled in at the point where the THROW statement occurs."

Former Member
0 Kudos

Ah, I *assumed* that since the runtime engine was doing the throwing that certain magic happens. So... just throwing it fills everything in. Sorry. I've never used for throwing - just catching.

code examples:

runtimeexception lrte

l_ds = CREATE datastore

if not isValid(l_ds) then throw lrte

another:

int li_rc

<some code>

if li_rc <> 1 then throw lrte

former_member190719
Active Contributor
0 Kudos

I'm not sure I would this standard practice.  What I tend to do is:

1.  Create a exception from throwable

2.  Actually instantiate the exception

3.  If I want to include script information I call PopulateError and then pull the information off the error object just before I throw the exception.

If you take a look at Power Building with Exceptions, the author there takes a user object inherited from the throwable class and a global function and puts something together that you can call in a single line when you want to raise an exception.

Former Member
0 Kudos

Agreed. Your is a much better (and often used by me) method. It's not my place to change what was written. It's been working for a long while without an error, so I'll leave it be but keep this thread handy if someone questions it.