lang: C++ custom exception class-used to handle exception information in self-made programming language

Preface

The first part lang: Summarize the syntax of 9 programming languages ​​to design the exception handling information in Suatin-lang . In fact, the exception information format of many languages ​​is very similar. This article uses C++ custom exception classes , and uses C++ exception handling to handle the exceptions of the upper language Suatin-lang.


Enumeration information

Enumerate all the error types, click this when printing exception information.

enum SuaError {
    
    
	NoDefinedError,				//未定义
	ZeroError,							//不该出现零的地方出现了零
	IOError,							//输入输出或文件操作出现问题
	ValueError,						//对已经有类型的变量传递了其他的类型
	OtherError                        //不属于上面错误的其他类型错误
};	

Display exception information

//自定义异常类省略(仮)

/*
异常信息有:
	dir				  文件路径
	lineNumber		  行数
	area              代码作用域
	lineContent       该行内容
	description       异常描述

*/
template<int error>
void SelectException(std::string dir,int lineNumber,std::string area,std::string lineContent,std::string description) {
    
    
	std::string errorString = "Traceback at : File \"" + dir + "\",line " + std::to_string(lineNumber)+" in <"+area+">\n\t"+lineContent+"\n";
	switch (error) {
    
    
	case NoDefinedError:
		errorString += "NoDefinedError : " + description + "\n";
		break;
	case ZeroError:
		errorString += "ZeroError : " + description + "\n";
		break;
	case IOError:
		errorString += "IOError : " + description + "\n";
		break;
	case ValueError:
		errorString += "ValueError : " + description + "\n";
		break;
	case OtherError:
	default:
		errorString += "OtherError : " + description + "\n";
		break;
	}

	//std::cout << errorString << std::endl;
	throw SuaExcept(errorString);

}




void main(void) {
    
    


	try {
    
    
		int a = 0;
		if (a == 0) {
    
    
			SelectException<NoDefinedError>("E:/Can/Suatin/test.sua", 
							120,"module","sum = sum + i", "i is not defined");
		}
	}
	catch (SuaExcept& e) {
    
    
		std::cout << e.what()<<std::endl;
	}

	system("pause");
	
}

Error demonstration of custom exception class

Because I want to pass an exception message to be printed in the past, so I write

class SuaExcept : public std::exception
{
    
    
	const char* error;
public:
	SuaExcept(std::string error) {
    
    
		this->error=error.c_str();
	}

	virtual const char* what()const throw() {
    
    
		return this->error;
	}
};

But it prints garbled characters! Debugging found that the error was indeed passed, but the custom exception class was called again when the exception was caught, and the e address obtained did not change, indicating that it was caught! But no content? ? ? ?

  • I don't know what the problem is.

Correct custom exception class

class SuaExcept : public std::exception
{
    
    
public:
	SuaExcept(std::string error) : exception(error.c_str()){
    
    	}
};

Output is

Traceback at : File "E:/Can/Suatin/test.sua",line 120 in <module>
        sum = sum + i
NoDefinedError : i is not defined

Guess you like

Origin blog.csdn.net/weixin_41374099/article/details/104114805