Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to handle improper input files
    primarykey
    data
    text
    <p>I am starting a project and I am wondering on best practices for handling errors during file input handling. My current plan for the project involves a process in <code>main</code> along the lines of:</p> <pre><code>unique_ptr&lt;Configuration&gt; config(initConfig(argc,argv)); unique_ptr&lt;InterfaceA&gt; a(initA(config)); // Do real work here </code></pre> <p>The initialization data for the Configuration class and all other classes will be contained in a header for their input files such as:</p> <pre><code>#ObjectA-1 </code></pre> <p>The <code>ObjectA</code> tells me I have a file meant to be turned into an object that fulfills <code>InterfaceA</code>, and the <code>1</code> tells me which specific implementation of that interface to use.</p> <p>My question is in error handling for functions such as <code>initConfig</code> and <code>initA</code>. Inside of those functions I will parse the first line of their respective files, and decode the above information. If, let's say, in <code>initA</code> I happen to get a file that does not have the appropriate header, be it <code>#ObjectB-3</code>, or no header at all. I see two ways to go about handling the error:</p> <ul> <li><p>Throw an exception that will be caught in main. This will allow me to print an error, then bypass other init functions via an error flag, and do any high level clean up I need to. The bad part of this is that my <code>main</code> is them mostly made of exception handling, which makes the code much harder to read.</p></li> <li><p>Print an error from inside the init function, then call <code>exit(EXIT_FAILURE)</code> and lean on my OS to do the cleanup of the previously allocated memory. This would likely lead to cleaner code and more local error handling.</p></li> </ul> <p>I personally would prefer the second if it wasn't for the use of the <code>exit</code> function. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload