Exceptions

Using exceptions to handle unexpected situations

// Project 07-01
// Name: GradeProject

#include <iostream>
#include <stdexcept>

char getLetterGrade();

using namespace std;

int main() {

char letterGrade=' ';
cout << "Please enter a score in percentage: ";
try {
letterGrade = getLetterGrade();
}

catch (const out_of_range& err) {
	cout << err.what();
	cout << endl;
	return 1;
	}

cout << "Letter Grade is " << letterGrade << endl;

    return 0;
}

char getLetterGrade()
{
	int grade{0};
	cout << "Please enter a grade (0-100) :";
	cin >> grade;
	if (grade < 0 || grade > 100) {
		throw out_of_range("Your percent must bee within 0 and 100, inclusive");
	}
	else if (grade >= 0 && grade <= 59) {
	return 'F';
	}
	else if (grade >= 60 && grade <= 69) {
	return 'D';
	}
	else if(grade >= 70 && grade <= 79) {
	return 'C';
	}
	else if(grade >= 80 && grade <= 89) {
	return 'B';
	}
	else {
	return 'A';
       }
}

Warpdrive Overheating class with exceptions

WarpDriveOverheating.h
#ifndef WARP_DRIVE_OVERHEATING_H
#define WARP_DRIVE_OVERHEATING_H
#include <stdexcept>
#include <iostream>
using namespace std;


class WarpDriveOverheating : public overflow_error
{
public:
	WarpDriveOverheating(): overflow_error("Warp drive has exceeded safe temperature tolerance.\nEjecting the Warp-Core")
	{
	}
};


#endif
main.cpp
// Project 07-02
// Name: SpaceShip Project

#include <iostream>
#include "WarpDriveOverheating.h"
using namespace std;

void testWarpDrive(int temperature);

int main() {

	try {
	for (int i = 10; i < 100; i +=10)
	{
	testWarpDrive(i);
	}
	}
	
	catch (const WarpDriveOverheating& err)
	{
		cout << err.what() << endl;;
		}

    return 0;
}


void testWarpDrive(int temperature)
{
	if (temperature > 80) {
	throw WarpDriveOverheating();
	}
	else if (temperature > 70) {
		cout << "Warning! Warp-core temerature is getting dangerously high!" << endl;
	}
	else {
		cout << "Temperature is within tolerance: ";
		cout << "Warp-Core Enclosure temperature is " << temperature << "°C" << endl;
	}
}