Exception Handling syntax

Howdy ! I need guidance on how to properly use exception handling. I want my function to take a single char and if its anything besides a letter, handle the exception and display an error message. Instead of the standard cout print statement if trouble is encountered. Here's what i have so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <ctype.h>
void checkFunction(char a);

int main() {
	using namespace std;
	char user_input;

	cout << "Enter A single character " << endl;
	cin >> user_input;
	checkFunction(user_input);

	system("pause");
	return 0;
}
void checkFunction(char a) {
	using namespace std;

	//isalpha() returns false/0 if char isn't a letter
	if (isalpha(a)) {
		cout << "good" << endl;
	}
	else 
		cout << "bad" << endl;
	}
	
Last edited on
I need guidance on how to properly use exception handling

1. Only throw when postconditions cannot be met (note: this implies that constructors always throw and that destructors never throw)
2. Throw by value, catch by reference.

take a single char and if its anything besides a letter, handle the exception and display an error message.

That is not a meaningful use of exception handling, but sure:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cctype>
#include <stdexcept>

void checkFunction(char a)
{
    if(!std::isalpha(a))
        throw std::runtime_error("bad character passed to checkFunction");
}

int main()
{
    std::cout << "Enter a single character\n";
    char user_input;
    std::cin >> user_input;
    try {
        checkFunction(user_input);
        std::cout << "good!\n";
    } catch(const std::exception& e)
    {
        std::cout << "Exception caught: " << e.what() << '\n';
    }
}

online demo: http://ideone.com/gOAPP
Last edited on
Im also new at exception handing, but dont you need to use try{} in order to use throw?
closed account (zb0S216C)
@Need4Sleep: No. try only tests for exceptions. However, it's possible to throw in a try block.

Wazzak
You don't particularly need to include <exception> I wrote this for class:

FileHandle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void FileHandle::openFile(const char* fileName, std::ios_base::open_mode mode)
{
  if (myStrcmp(inFileName, fileName) == 0 || myStrcmp(outFileName, fileName) == 0)
  {
    throw "File with that name already open"
  }
  else if (mode == std::ios::in || mode == (std::ios::in | std::ios::binary))
  {
    if (inFile.is_open())  throw "Input file already open";
    inFile.open(fileName);
    if (!inFile) throw "Couldn't open input";

    inFile.seekg (0, std::ios::end);
    inFileSize = inFile.tellg();
    inFile.seekg (0, std::ios::beg);
		
    myStrcpy(inFileName, fileName);
    inFileMode = mode;
  }
  else if (mode == std::ios::out || mode == (std::ios::out | std::ios::binary))
  {
    if (outFile.is_open()) throw "Output file already open";
    outFile.open(fileName);
    myStrcpy(outFileName, fileName);
    outFileMode = mode;
  }
  else throw "Unrecognized open mode"
}


main
1
2
3
4
5
6
7
8
9
10
FileHandle file;
try
{
  file.openFile("input.txt", ios::in);
}
catch (const char* error)
{
  cout << "Error: " << error << endl;

}


I later decided that I prefer a function simply returning false instead of being a void with a throw.

The point is that if a throw occurs, then the entire function stops and we go back to main. This way I didn't try to seekg() on a file that wasn't open.
Last edited on
Topic archived. No new replies allowed.