C++ has a different exception specifier

这个报错:

error: declaration of ‘virtual const void* int8EntroyCalibrator::readCalibrationCache
(std::size_t&)’ has a different exception specifier

正常来说就是你声明的函数定义和真正函数实现的时候,参数或者函数类型不一致,仔细检查,把参数复制成一样的即可,但是我的确定不存在不一致的问题,
继承了基类,实现了对应的虚函数,最后的问题是出在throw()和noexcept两个关键字上,二者的目标都是限制函数的异常安全性,使函数不抛出任何异常,但是不能混用,这个其实就是c++11和c++98的定义的坑,
https://stackoverflow.com/questions/39188919/different-exception-specifier-with-g-6-2
Are you using C++11 or later?

The original operator new() declarations in C++98

throwing:   
void* operator new (std::size_t size) throw (std::bad_alloc);
 
nothrow:
void* operator new (std::size_t size, const std::nothrow_t& nothrow_value) throw();
 
placement:
void* operator new (std::size_t size, void* ptr) throw();

Have been changed in C++11 to use noexcept keyword:

throwing:   
void* operator new (std::size_t size);
 
nothrow:    
void* operator new (std::size_t size, const std::nothrow_t& nothrow_value) noexcept;
 
placement:  
void* operator new (std::size_t size, void* ptr) noexcept;

基类中的函数后面有noexcept=0修饰,noexcept=0是一种特殊的异常说明符,它表示函数是一个纯虚函数(即抽象函数),并且不会抛出异常。纯虚函数是指在基类中声明但没有实现的虚函数,它的实现由派生类提供。

当在基类中声明一个纯虚函数时,可以使用noexcept=0来指定该纯虚函数不会抛出异常。这样,派生类在实现该纯虚函数时也需要保持不会抛出异常的性质,否则会导致编译错误

所以,派生类在实现时也要加上noexcept关键字表示不会抛出异常,即可解决问题:

//声明时:
bool getBatch(void* bindings[], const char* names[], int nbBindings) noexcept override;
//实现时:
bool int8EntroyCalibrator::getBatch(void* bindings[], const char* names[], 
int32_t nbBindings) noexcept
{
      .......
      }

### 解决 Java 异常的方法 #### 处理 `EmptyStackException` `java.util.EmptyStackException` 表示尝试从空堆栈中弹出或查看顶部元素的操作失败。为了避免这种情况,在执行这些操作之前应先检查堆栈是否为空[^2]。 ```java if (!stack.isEmpty()) { Object topElement = stack.pop(); } ``` #### 应对 `FileNotFoundException` 对于 `java.lang.FileNotFoundException`,确保指定的文件存在并且路径正确至关重要。可以预先验证文件的存在性和可访问性来预防此类异常的发生[^3]。 ```java File file = new File(filePath); if (file.exists() && !file.isDirectory()) { try (BufferedReader br = new BufferedReader(new FileReader(file))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } else { System.err.println("The specified file does not exist or is a directory."); } ``` #### 防范 `ProtocolException` 针对 `java.net.ProtocolException` 的情况,遵循正确的网络协议规则非常重要。例如,在发送 HTTP 请求时设置适当的内容长度头信息可以帮助防止因违反HTTP/1.1 协议而引发此异常[^4]。 ```java HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection(); connection.setRequestMethod("POST"); // 设置 Content-Length header to match the actual content length. byte[] postDataBytes = postData.toString().getBytes("UTF-8"); connection.setFixedLengthStreamingMode(postDataBytes.length); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); try (OutputStream out = connection.getOutputStream()) { out.write(postDataBytes); } ``` 通过上述措施可以在很大程度上减少相应类型的异常发生的可能性,并提高应用程序稳定性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我现在强的可怕~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值