Throw an exception, then throw / send an exception and continue

Thus, the title may be a little misleading, but what I wanted to accomplish was reading an array of files and then merging them into one, where I am now.

The problem is that I have a catch that is looking for a "FileNotFoundException" exception when it is called. I want to continue the try statement (using "continue"), but let the user know that the file is missing.

My setup is a class that is called from the form (it is in the form where the error should be displayed)

I was thinking of creating an event that can be logged in my form, but is that so?

public void MergeClientFiles(string directory) { // Find all clients Array clients = Enum.GetValues(typeof(Clients)); // Create a new array of files string[] files = new string[clients.Length]; // Combine the clients with the .txt extension for (int i = 0; i < clients.Length; i++) files[i] = clients.GetValue(i) + ".txt"; // Merge the files into directory using (var output = File.Create(directory)) { foreach (var file in files) { try { using (var input = File.OpenRead(file)) { input.CopyTo(output); } } catch (FileNotFoundException) { // Its here I want to send the error to the form continue; } } } } 
+4
source share
5 answers

You want the method to do its job and report problems to the user, right? Then Oded suggested the right things. With a little modification, the code might look like this:

  public List<string> MergeClientFiles( string path ) { // Find all clients Array clients = Enum.GetValues( typeof( Clients ) ); // Create a new array of files string[] files = new string[clients.Length]; // Combine the clients with the .txt extension for( int i = 0; i < clients.Length; i++ ) files[i] = clients.GetValue( i ) + ".txt"; List<string> errors = new List<string>(); // Merge the files into AllClientData using( var output = File.Create( path ) ) { foreach( var file in files ) { try { using( var input = File.OpenRead( file ) ) { input.CopyTo( output ); } } catch( FileNotFoundException ) { errors.Add( file ); } } } return errors; } 

Then, in the caller, you simply check to see if MergeClientFiles returns a non-empty collection.

+3
source

You can collect exceptions in List<FileNotFoundException> and at the end of the iteration, if the list is not empty, throw a custom exception, assigning this list to the corresponding element.

This will allow any code that calls above to catch your custom exception, iterate through FileNotFoundException and notify the user.

+1
source

you can define the delegate that you pass as an argument to your method.

 public delegate void FileNotFoundCallback(string file); public void MergeClientFiles(string directory, FileNotFoundCallback callback) { // Find all clients Array clients = Enum.GetValues(typeof(Clients)); // Create a new array of files string[] files = new string[clients.Length]; // Combine the clients with the .txt extension for (int i = 0; i < clients.Length; i++) files[i] = clients.GetValue(i) + ".txt"; // Merge the files into directory using (var output = File.Create(directory)) { foreach (var file in files) { try { using (var input = File.OpenRead(file)) { input.CopyTo(output); } } catch (FileNotFoundException) { // Its here I want to send the error to the form callback( file ); continue; } } } } 
+1
source

For some inspiration, check out the documentation for the new parallel constructs in C #, such as Parallel.For and Reactive Framework (gy).

In the first case, exceptions are thrown in an AggregateException , in Rx, exceptions are passed through the callback interface.

I think I prefer the approach used in Parallel.For, but choose what works best for your scenario.

0
source

Instead of catching a FileNotFoundException , you should actively check to see if the file exists and then not try to open it if it is not.

You can change the method to return a list of merged files, a list of missing files, or a list of all files along with an indicator if they were merged or missing. Returning one list gives the caller the opportunity to process the missing files at the same time and know how many were missing, and not one by one, as is the case with an event or callback.

0
source

Source: https://habr.com/ru/post/1435786/


All Articles