Catching Exceptions?

I'm having some trouble understanding how I should correctly catch exceptions from the things I have been reading around.

If I put an exception handler that will allow compile but not catch exceptions, I can get the standard handler to show me the exceptions being caused.

two of the main ones so far as FileNotFoundException and IndexOutOfBoundsException

how do I go about catching these correctly in my exception handler?

So far I have this:

1
2
3
catch(Exception^ e){
 MessageBox::Show("File Error!","File Open", MessageBoxButtons::OK,MessageBoxIcon::Error);
}


but how do I check the incoming exception type in order to select which error message to display?

According to the MSDN Library, FileNotFoundException has value "0x80070002"

how do I check for this value?

Thanks
}
Last edited on
I think I have solved this problem with the following code, I am however getting a warning, can anybody please help me with a solution?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public:
	array<String^>^ loadFile(void){

		try
				{
					System::String^ fileName = Environment::CurrentDirectory + "\\pins.txt";
					array<System::String^>^ theLines = System::IO::File::ReadAllLines(fileName);
					return theLines;
				}

		catch( System::IO::FileNotFoundException^){
			MessageBox::Show("File Not Found!","Exception", MessageBoxButtons::OK,MessageBoxIcon::Error);
		}
		catch( System::IndexOutOfRangeException^){
			MessageBox::Show("File Not Formatted Correctly.","Exception", MessageBoxButtons::OK,MessageBoxIcon::Error);
		}

	}


This gives a warning when compiling:

: warning C4715: 'myProject::Form1::loadFile' : not all control paths return a value


any suggestions?

I don't want my exceptions to return anything.
Don't catch them then, have whoever is calling the function catch them.
Thanks, that worked a treat!
Topic archived. No new replies allowed.