on 08-04-2015 6:09 PM
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?
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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."
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
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.
User | Count |
---|---|
86 | |
10 | |
10 | |
9 | |
7 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.