squareErr
#pragma once
#include <stdexcept>
using namespace std;
class squareErr : public exception
{
public:
squareErr();
~squareErr();
const char *what() const;
};
squareErr.cpp
#include "stdafx.h"
#include "squareErr.h"
#include <iostream>
using namespace std;
squareErr::squareErr()
{
}
const char * squareErr::what() const{
return "平方根参数不能为负";
}
squareErr::~squareErr()
{
}
main.cpp
// 异常处理-23期-92-子羽.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <stdexcept>
#include "squareErr.h"
#include <cmath>
using namespace std;
float square(float num) {
if (num < 0)
{
throw squareErr();
}
return sqrt(num);
}
int _tmain(int argc, _TCHAR* argv[])
{
try
{
square(-10);
}
catch (squareErr ex)
{
cout << ex.what() << endl;
}
return 0;
}
网友评论