none
Continuing execution even after there is a read error RRS feed

  • Question

  • i am writing a code   which reads mp3 file database of over a terabyte and  stores at the music information in a excel file .

    The problem is that at once a corrupt file is encountered the program execution stops then and there only . 

    Is  there any way by which we  can continue the program execution skipping the corrupted files on the way   .

    i am attaching the code here with 

    try
                {
                    DirectoryInfo dirInfo = new DirectoryInfo(path);
                    using (StreamWriter file = new StreamWriter("fileinfo.csv", false))
                    {
                            file.WriteLine("Title" + "," + "Singer" + "," + "Album" + "," + "year" + "," + "Comment");
                            foreach (FileInfo f in dirInfo.GetFiles("*.mp3", SearchOption.AllDirectories))
                            {
                       
                                byte[] b = new byte[128];
                                string sTitle;
                                string sSinger;
                                string sAlbum;
                                string sYear;
                                string sComm;
                        
    
    
                            
                                    FileStream fs = new FileStream(f.FullName, FileMode.Open);
                                    fs.Seek(-128, SeekOrigin.End);
                                    fs.Read(b, 0, 128);
    
                                    bool isSet = false;
                                    String sFlag = System.Text.Encoding.Default.GetString(b, 0, 3);
                                    if (sFlag.CompareTo("TAG") == 0)
                                    {
                                       // System.Console.WriteLine("Tag   is   setted! ");
                                        isSet = true;
                                    }
    
    
                                    if (isSet)
                                    {
                                        song_count++;
                                        
                                        //get   title   of   song; 
                                        sTitle = System.Text.Encoding.Default.GetString(b, 3, 30);
                                        file.Write(sTitle + ",");
                                
                                        //get   singer; 
                                        sSinger = System.Text.Encoding.Default.GetString(b, 33, 30);
                               
                                        file.Write(sSinger + ",");
                                        //get   album; 
                                        sAlbum = System.Text.Encoding.Default.GetString(b, 63, 30);
                                
                                        file.Write(sAlbum + ",");
                                        //get   Year   of   publish; 
                                        sYear = System.Text.Encoding.Default.GetString(b, 93, 4);
                               
                                        file.Write(sYear + ",");
                                        //get   Comment; 
                                        sComm = System.Text.Encoding.Default.GetString(b, 97, 30);
                                
                                        file.Write(sComm);
                                    }
    
                                    file.WriteLine();
                                    
                                }
                            Console.Out.WriteLine("Meta Data for {0} songs created in the database " + song_count);
                            file.Close();
                          
                    }
      
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error reading files ");
                    Console.WriteLine("HelpLink = {0}", ex.HelpLink);
                    Console.WriteLine("Message = {0}", ex.Message);
                }
    Whenever there is any corrupt music file on the disk the program execution reaches the catch block and stops . and all subsequenet good files are not read 

    arka Bhattacharya

    Friday, January 25, 2013 9:42 PM

Answers

  • Hi,

    please put a try. catch block inside the code where you are reading the file and if you get exception just continue the reading..

    foreach( var file in files)

    {

    try

    {

    }

    catch

    {

    }

    }

    please do not throw the exception..


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful". CRMHUNT http://crmhunt.com

    • Proposed as answer by CRM HUNT Saturday, January 26, 2013 9:35 AM
    • Marked as answer by arka_b1990 Friday, February 1, 2013 9:50 AM
    Saturday, January 26, 2013 9:34 AM

All replies

  • What line of code is dying?

    JP Cowboy Coders Unite!

    Friday, January 25, 2013 10:28 PM
  • you already use a try...catch, but onyl for the entire code.

    You have to put a seperate try...catch around each single write/read that you want to be protected (wich should be all anyway - even writes can fail!). You should somehow log the failed reads in the outputt file/window.

    • Proposed as answer by JMCF125 Friday, January 25, 2013 10:46 PM
    Friday, January 25, 2013 10:39 PM
  • More detailed. If these lines

    FileStream fs = new FileStream(f.FullName, FileMode.Open);
    fs.Seek(-128, SeekOrigin.End);
    fs.Read(b, 0, 128);

    (or any other lines) throw an exception (errors far beyond what a programm can handle. You only work with exceptions!) the programm jumps directly to the first catch whose class condition matches it's class.

    In this case, the catch block at the end, wich catches ALL exceptions (because all specific exceptions are dervied from exception, the base class).

    You want to catch certain file reading errors (and only certain file readign errors. Don't use exception unless you want to handle "outofmemory" exceptions) that happen inside the loop. best startign point is propably to catch all IOExceptions (in a way that either writes a error message into the file, or at least skips the writing step).


    Friday, January 25, 2013 10:51 PM
  • Hi,

    please put a try. catch block inside the code where you are reading the file and if you get exception just continue the reading..

    foreach( var file in files)

    {

    try

    {

    }

    catch

    {

    }

    }

    please do not throw the exception..


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful". CRMHUNT http://crmhunt.com

    • Proposed as answer by CRM HUNT Saturday, January 26, 2013 9:35 AM
    • Marked as answer by arka_b1990 Friday, February 1, 2013 9:50 AM
    Saturday, January 26, 2013 9:34 AM
  • For a more detailed view and a few best pracices regarding exception handling, I liked this article:

    http://www.codeproject.com/Articles/9538/Exception-Handling-Best-Practices-in-NET

    Saturday, January 26, 2013 5:06 PM
  • very helpful article

    arka Bhattacharya

    Friday, February 1, 2013 9:51 AM