新手错误处理有关问题

新手异常处理问题
//Test the Exception

#include <iostream>
#include <string.h> 

using namespace std;

double Divide(double a,double b){
if(b == 0){
// string charMsg = "the 0 can't be devided!"; //异常的内容 
// string* pMsg = &charMsg;  //异常的内存地址 

//char* pMsg = "0不能做被除数";  //不被建议的用法,实际为const char* 为了向下兼容c的设计 
char arrMsg[] = "0不能是被除数";
int a = sizeof(arrMsg); 
cout << "a=" <<a <<endl;
throw arrMsg; //抛出异常 
}

return (double)a/b;
}

int main(){
try{
double result = Divide(3,0);
cout << "除法运算结果为:" << result <<endl; 
}
//catch(string* pMsg){ //指向异常的指针 
catch(char arrMsg[]){
cout << "异常发生在:" << arrMsg << endl; //指出异常的内存地址 
}

return 0;
}[code=c][code=c]
------解决思路----------------------
不能直接抛出一个数组, 因为会退化为指针, 要么直接抛指针, 要么用一个结构体包装一下.

"//不被建议的用法,实际为const char* 为了向下兼容c的设计 ", 这是谁说的.这么叼.
------解决思路----------------------
引用:
不能直接抛出一个数组, 因为会退化为指针, 要么直接抛指针, 要么用一个结构体包装一下.

"//不被建议的用法,实际为const char* 为了向下兼容c的设计 ", 这是谁说的.这么叼.


C++ draft n4269
2.13.5
8 Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. A narrow
string literal has type “array of n const char”, where n is the size of the string as defined below, and has
static storage duration (3.7).

当 array of n const char 转化为指针时,得到的是 const char * 。
------解决思路----------------------
还有一个问题:
double不能那样子同0比较。
------解决思路----------------------
在占用内存空间较大的局部数组声明的前面加static将其从堆栈数据段挪到全局数据段即可避开因局部数组大小超过默认堆栈大小1MB造成程序不能正常运行的问题。

当然楼主的代码不是局部数组过大的问题;而是局部数组默认分配在栈中。
通过static修饰,将其挪到全局变量区(全局数据段)中,就不会发生指向它的指针在退出函数后失效的问题了。
------解决思路----------------------
对以基本硬件异常如 divided by 0,
你只需要启动硬件异常,就可以检测到了
C++ 库,可以设置为,用或者不用异常,硬件异常也是可以设置的