✅ How should I structure 'exit points' in my program?

Hey guys. I'm working solo on a project that involves opening an Excel file, sorting through and transforming into a DataSet. I'm not very experienced and unfortunately am working on this completely solo. This will then be used to create invoices on an ERP; as such, it'll be imported as a .dll into that program. I have a main class inherits from one of the ERPs native classes and it calls methods from another class I wrote myself. That second class has methods to open an Excel file, sort through it with some rules and transforming the results into a DataSet I'll then use to make the invoices. I've tried my best to write fallbacks in the code in case the file isn't present, user doesn't have permission to access it, etc. Question 1: if something goes wrong in the secondary class (for example Excel file is already in use), how do I make it so the whole thing stops. Right now I'm just "return"ing back out of the secondary class to the main one, but that one then has nothing to stop it going forwards. Question 2: I currently have all the secondary classes methods in its constructor meaning that I just have to instanciate it in the main class, and the whole Excel manipulation and DataSet creation happens at once. Would it be a better idea to make those methods public, remove from constructor and call them one by one on the main class? Thank you kindly!!
6 Replies
Anton
Anton2y ago
1. you generally throw an exception, or return with a non-zero exit code if your system allows it. if you control the flow of the main method, you can check the return value of the function that you called, and if it returns say false or null, you return with a non zero exit code. 2. constructors are generally for initialization, so it's best to create the context and then call some other method on it. it's funky to put logic in there, generally inconvenient. but do whatever suits you. you could just use static functions too, and pass the parameters explicitly. also, doing logic in methods is generally more flexible, because you can return things you can only return nothing from a constructor or throw exceptions
xarop_pa_toss
xarop_pa_tossOP2y ago
Thanks for the answers. Regarding #2 I think I have it setup as you mention.
public Class()
method1()
if method1 result == null;
return;
method2()
if method2 result == null;
return;
public Class()
method1()
if method1 result == null;
return;
method2()
if method2 result == null;
return;
Think my main issue is with Question #1. Are you familiar with helpful documentation on the matter? Or somewhere I can read about how to implement things as exit codes, etc?
Anton
Anton2y ago
the exit code is just the integer you return from main if it's non-zero, that means there was an error 0 means ok if you throw an exception, and it propagates all the way through the call stack, the runtime will return a non-zero exit code automatically, and display the exception and the stack trace I don't think there's much else to know about this
xarop_pa_toss
xarop_pa_tossOP2y ago
thanks for the answers. I did some digging and found a nice explanation. I didn't realise you could put something in a try-catch block, call methods inside the block and throw exceptions inside those methods. very interesting and useful stuff! Thanks @AntonC
Anton
Anton2y ago
np
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity. Closed!
Want results from more Discord servers?
Add your server