1. #1
    adrianhates's Avatar
    Registered
    23/01/06
    Posts
    2,115
    iTrader
    0
    Mentioned
    0 Post(s)
    Reputation
    23/23

    C++ : Exceptions worden niet opgevangen / of ander probleem

    Yo,

    Ik ben bezig met een kleine oefening voor school en ik probeer daar excepties in te verwerken. Het concept v/d oefening is very basic :

    1) Bron bestand wordt gevraagd
    2) Doel bestand wordt gevraagd
    3) Bron bestand inlezen
    4) Bron kopieren naar nieuw doel bestand

    Ik dacht hier een exceptie te throwen als
    a) Het invoerbestand niet bestaat of niet geopend kan worden
    b) Het uitvoerbestand niet aangemaakt / geopend kan worden
    c) Niet het einde van het bestand werd bereikt bij het kopieren.

    Is het al juist om te denken dat een uitvoer bestand eventueel niet geopend kan worden? Bvb door onvoldoende rechten? Is het type van de catch misschien niet correct?

    Als ik foutieve invoer doe, bvb "a" en "b" dan kopieert hij niet , maar toont ook de excepties niet.
    Hierbij al vast mijn code. De bestanden die ik open zitten voor het simpele testgegeven in de zelfde folder als de .cpp en .exe file.

    link naar pastebin indien gemakkelijker : [C++] C++ - Pastebin.com
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    void openBestanden(string bron_bestand, string doel_bestand,
    					ifstream & input_stream, ofstream & output_stream){
    	
    	input_stream.open(bron_bestand.c_str());
    	if(!input_stream.is_open())
    		throw "Bronbestand " + bron_bestand + " kon niet worden geopend!";
    		
    	output_stream.open(doel_bestand.c_str());
    	if(!output_stream.is_open())
    		throw "Doelbestand " + doel_bestand + " kon niet worden geopend!";
    		
    }
    
    
    int main(){
    	
    	string bron_bestand;
    	string doel_bestand;
    	
    	ifstream input_stream;
    	ofstream output_stream;
    	
    	cout << "Geef het bronbestand op : " << endl;
    	cin >> bron_bestand;
    	
    	cout << endl << "Geef naam nieuw doelbestand op : " << endl;
    	cin >> doel_bestand;
    	cout << endl;
    		
    	try{
    		
    		// open de bestanden en streams
    		openBestanden(bron_bestand, doel_bestand, input_stream, output_stream);
    		
    		string line;
    		int line_number = 1;
    		getline(input_stream,line);
    		while(!input_stream.fail()){
    			
    			cout << "Lijn nummer " << line_number << " met inhoud '" << line << "' wordt gekopieerd .. " << endl;
    			output_stream << line << endl;
    			line_number++;
    			getline(input_stream,line);
    			
    		}
    		
    		if(!input_stream.eof())
    			throw "Het bronbestand kon niet helemaal gekopieerd worden .. ";
    		
    		// close streams
    		input_stream.close();
    		output_stream.close();
    		
    	}
    	catch(const char * ex){
    		
    		cout << ex << endl;
    		
    	}
    	catch(...){
    		
    	}
    		
    	system("pause");
    	return 0;
    		
    }
    Thx!
    no votes  

  2. #2
    forloRn_'s Avatar
    Registered
    23/11/03
    Location
    Landeurp
    Posts
    1,791
    iTrader
    0
    Mentioned
    0 Post(s)
    Reputation
    10/17
    Wel, je throwt een string en je catcht een const char *. Lijkt me logisch dat dat niet werkt.
    no votes  

  3. #3
    adrianhates's Avatar
    Registered
    23/01/06
    Posts
    2,115
    iTrader
    0
    Mentioned
    0 Post(s)
    Reputation
    23/23
    A ma gvd! Ik dacht dus echt da da een Cstring was ..
    Als ik de concatenatie weglaat werkt het wel ..

    Lesson learned! Wel spijtig dat de compiler hier geen fout op geeft.
    no votes  

  4. #4
    blackrabbit's Avatar
    Registered
    25/07/02
    Location
    Brussel
    Posts
    1,001
    iTrader
    5 (100%)
    Mentioned
    0 Post(s)
    Reputation
    2/2
    Is toch ook geen fout?
    no votes  

  5. #5
    adrianhates's Avatar
    Registered
    23/01/06
    Posts
    2,115
    iTrader
    0
    Mentioned
    0 Post(s)
    Reputation
    23/23
    Quote Originally Posted by blackrabbit View Post
    This quote is hidden because you are ignoring this member. Show
    Is toch ook geen fout?
    Klopt maar bij
    throw "Bronbestand " + bron_bestand + " kon niet worden geopend!";

    moet ik
    Code:
            catch(const string ex){
    		
    		cout << ex << endl;
    		
    	}
    gebruiken blijkbaar.. Ik heb het net even getest ..
    no votes  

  6. #6
    forloRn_'s Avatar
    Registered
    23/11/03
    Location
    Landeurp
    Posts
    1,791
    iTrader
    0
    Mentioned
    0 Post(s)
    Reputation
    10/17
    Het is juist omdat je die concatenatie doet met een std::string dat je als resultaat een std::string krijgt: klikkie
    no votes  

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Log in

Log in