✅ 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_toss2y 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_toss2y 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
More Posts
❔ Does this method cause boxing of value types?I was looking through the list<T> class and noticed that the Contains<T>(T item) method attemps to c❔ Help with realization Division by zero in wpf projectI want to throw out the error when dividing by zero. Since "Compute" gives infinity when divided by Get data from a model and display it as dropdown on client side in RazorHi, how do I retrieve data in a database from a model and show it in as my dropdown values in my raz❔ Help with DI, i have different connection strings but same behavior for query DBHi, i want to instance the same query service but with different configuration and i have no idea ho❔ Help with stream reading and writing and displaying information at a certain timeSo I am currently doing stream writing and reading and I need it when the user types the student ID ✅ Foreign key not workingHi, I tried making a foreign key in my table, to another table which I made but it does not work as ✅ Shorten data querying lineHi, how do I shorten this line that's querying for data? Like, instead of putting them into one long❔ Blazor JS Interop - Return RTCPeerConnection from js to Blazor in order to close itHi, I've quite a complex task: In my Blazor application I use javascript to read webrtc data produce✅ Authentication with CookiesI'm playing around with cookies to get a better understanding. My code: ``` var builder = WebApplic❔ need help with visual studio communityhi i have multipul q 1- i have mac is there way to have community on it 2- please hoe to make 3 bu