C ++ exception is thrown study notes -throw

C ++ exception is thrown study notes -throw

Foreword

         In the process of learning C ++ data structure, often seen following code block:

template <class T>
T LinkList<T>::Get( int i)
{ //初始化
	Node<T> *p=first->next;
	int j=1;
	while (j!=i) && (p!=NULL)
	{
		p=p->next;
		j++;
	}
	if(p==NULL) throw “位置非法”;
	return p->data; 
}

         In which the teacher to explain throw is: if the condition, an exception is thrown. But after the suit, but the following error message appears:
Here Insert Picture Description
         So they throw on the specific use of had a curiosity, pretending to text to remember.

C ++ exception handling

         In the C ++ language, usually try, throw and catch statements to implement exception handling, for example:

#include <iostream>
using namespace std;

void ThrowError()
{
	int a;
	cin >> a;
	if (a == 0) throw 1;
	else if (a == 1) throw"字符串:输入的为1";
	else throw 100;
	cout << "throw后面的语句" << endl;
}

int main()
{
	try
	{
		ThrowError();
	}
	catch (int e)
	{
		switch (e)
		{
		case 1:
			cout << "输入的为0" << endl;
			break;
		default:
			cout << "输入的既不是0也不是1" << endl;
			break;
		}
	}
	catch (const char* e)
	{
		cout << e << endl;
	}
	cout << "catch后的语句" << endl;
	return 0;
}

         Operating results:
Here Insert Picture Description
         Next, various parts of the code block one by one explains

1. try {}
         statements within the try to follow in brackets behind the protection of the code, if the program execution to try statement according to the normal hearing cis content try statement is executed. If the try statement is executed normally, the catch in the back with a try statement is not executed. After the execution of the program after the statement is executed instead try to follow the last catch statement. On the contrary, if the try statement is not executed properly, the statement in the catch executed.

2. throw
         The basic syntax throw of:

throw 表达式;//表达式可以是基本数据类型,也可以是类

         An exception is thrown by the statement. Examples can be found by running in the try block, the statement throws an exception if successful, it will no longer throw after being executed.

3. catch
         if the try block during execution of exception is thrown, then the exception is thrown immediately jump to the first "Exception Types" and the type of exception thrown catch block matching is performed , and then executing the jump a catch behind the last block continues.
         If an exception is not caught, then the program will immediately be terminated, the situation appeared foreword.

postscript

         throw thrown practices in the face of the need to apply and release dynamic memory involves pointer manipulation, we can more effectively find, such as wild pointers, pointer hanging and other issues. Therefore record.

Released three original articles · won praise 1 · views 96

Guess you like

Origin blog.csdn.net/weixin_45817309/article/details/105215427