1.在C++ 层如果是自己写的代码 或者调用别人的方法,记得要 catch 异常,如果不try catch ,在java层是 try catch 不住的,肯定会挂掉
2.如果异常需要往外抛 给java 层 ,一定要按照java 层抛异常的方式
try{
}catch(const Exception& e){
env->FindClass("java/lang/Exception");
env->ThrowNew(je,e.what());
}
3.如果是自己写的NDK 代码,最好抛自己写的异常 ,声明异常
4.如果是做 C / C++ , 最好抛系统定义好的异常,或者继承系统的异常
5. 系统异常的体系 Exception 基类 ( #include <stdexcept>)
转载:http://blog.csdn.net/wxqian25/article/details/14230523
对std::out_of_range抛出异常进行处理 ,头文件stdexcept
#include <iostream>
#include <vector>
#include <stdexcept>
using namespace std;
int main() {
vector <int> a;
a.push_back(1);
try {
a.at(1);
}
catch (std::out_of_range &exc) {
std::cerr << exc.what() << " Line:" << __LINE__ << " File:" << __FILE__ << endl;
}
return EXIT_SUCCESS;
}
这样就能知道在第几行和哪个文件中了。
说明编辑
C++异常类,继承自logic_error,logic_error的父类是exception。属于运行时错误,如果使用了一个超出有效范围的值,就会抛出此异常。也就是一般常说的越界访问。定义在命名空间std中。
使用时须包含头文件 #include<stdexcept>
out_of_range例子
// out_of_range example
#include<iostream>
#include<stdexcept>
#include<vector>
using namespace std;//或者用其他方式包含using std::logic_error;和using std::out_of_range;
int main (void)
{
vector<int> myvector(10);
try
{
myvector.at(20)=100; // vector::at throws an out-of-range
}
catch (out_of_range& oor)
{
cerr << "Out of Range error: " << oor.what() << endl;
}
getchar();
return 0;
}
myvector只有10个元素,所以myvector.at(20)就会抛出out_of_range异常。