我想知道应该如何以正确的方式处理域异常?
我的所有消费者代码是否都应该包装到 try、catch 块中,或者我应该抛出一个异常,该异常将由适当的FaultConsumer 处理?
考虑这两个示例:
示例 1 - 整个操作被包装到 try...catch 块中。
public async Task Consume(ConsumeContext<CreateOrder> context)
{
try
{
//Consumer that creates order
var order = new Order();
var product = store.GetProduct(command.ProductId); // check if requested product exists
if (product is null)
{
throw new DomainException(OperationCodes.ProductNotExist);
}
order.AddProduct(product);
store.SaveOrder(order);
context.Publish<OrderCreated>(new OrderCreated
{
OrderId = order.Id;
});
}
catch (Exception exception)
{
if (exception is DomainException domainException)
{
context.Publish<CreateOrderRejected>(new CreateOrderRejected
{
ErrorCode = domainException.Code;
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
示例 2 …
我有以下代码:
#include <variant>
#include <optional>
#include <cstdint>
#include <iostream>
#include <type_traits>
using DataType_t = std::variant<
int32_t,
std::optional<uint32_t>
>;
constexpr uint32_t DUMMY_DATA = 0;
struct Event
{
explicit Event(DataType_t data)
: data_(data)
{}
template <class DataType>
std::optional<DataType> getData() const
{
if (auto ptr_data = std::get_if<DataType>(&data_))
{
return *ptr_data;
}
return std::nullopt;
}
DataType_t data_;
};
int main() {
auto event = Event(DUMMY_DATA);
auto eventData = event.getData<int32_t>();
if(!eventData) {
std::cout << "missing\n";
return 1;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
代码非常简单明了,但我遇到了一个奇怪的行为。当我使用 gcc 8.2 编译它时,返回代码为 …
嘿我写简单的应用程序,这是两个玩家可以互相聊天,基本上玩.
我的应用可以根据收到的数据类型执行不同的操作.
例如,如果玩家1向玩家2发送消息,则玩家2的客户端上的应用程序识别出它是消息类型,并且激发更新GUI的合适事件.
另一方面,如果玩家1进行移动,则玩家2的客户端应用程序识别出它是移动类型并且做适当的事件.
所以它是数据的缓冲区
Byte[] buffer = new Byte[1024];
Run Code Online (Sandbox Code Playgroud)
有可能写入buffer[0]数据类型(1 - MSG,2 - MV),其余数据写入其余字节吗?或者是否有更好的方法来实现这个功能?
我尝试从appsettings.json获取我的部分,然后将其绑定到MongoSettings类的实例,但是我有一个例外:
“无法创建类型为'System.String'的实例,因为它缺少公共的无参数构造函数。”
很奇怪,因为我使用相同的方法来获取jwt设置。
请看一下:
var jwtSettings = Configuration.GetSection("jwt").Get<JwtSettings>(); //it works
var mongoSettings = Configuration.GetSection("mongo").Get<MongoSettings>(); //it doesn't
Run Code Online (Sandbox Code Playgroud)
appsettings.json
"Jwt": {
"issuer" : "localhost:5000",
"expiryMinutes" : 60,
"key" : "das#@4SD120847@12313"
},
"Mongo": {
"connection:" : "mongodb://localhost:27017",
"database" : "MemoTime"
}
Run Code Online (Sandbox Code Playgroud)
MongoSettings:
public class MongoSettings
{
public string Connection { get; set; }
public string Database { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
JwtSettings:
public class JwtSettings
{
public string Key { get; set; }
public string ValidIssuer { get; set; }
public int ExpiryMinutes …Run Code Online (Sandbox Code Playgroud) 我的代码有问题。如果我用 -O0 或 -Og 编译它,它似乎工作正常。
但是,如果我使用任何其他标志,如 -Os、-O1 等,则它不起作用。如何找到编译器优化的内容?
编译器 arm-none-eabi-g++ 8.3.1
源代码:https : //github.com/bielu000/stm32-libopencm3/tree/uart_not_working_version
我添加了到 repo 的链接,因为很难将整个代码放在这里。主要代码:src/app/src
我认为问题出在 server_run 函数中。
看看屏幕。
在左侧(它有效)->
优化:-O1,
属性((optimize("-O0"))) void server_run();
在右侧(它不起作用)->
优化:-O1,
void server_run();
我没有看到任何在优化(右)版本中获取缓冲区容量的调用。但为什么?
函数体
extern "C" {
#include <libopencm3/stm32/usart.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/cm3/nvic.h>
}
#include <server.hpp>
#include <stdint.h>
#include <ring_buffer.hpp>
#include <Os.hpp>
#include <Timer.hpp>
#include <target.h>
static uint8_t w_buffer[1024]; // write buffer
static uint8_t r_buffer[1024]; // read buffer
utils::containers::RingBuffer write_rb{w_buffer, sizeof(w_buffer)};
utils::containers::RingBuffer read_rb{r_buffer, sizeof(r_buffer)};
static void sendData()
{ …Run Code Online (Sandbox Code Playgroud) 我想弄清楚为什么这段代码不能编译?我已经创建了用户定义的析构函数,因此不应创建移动构造函数和移动赋值运算符。那么为什么复杂化失败,说我的班级不符合要求?
#include <iostream>
#include <concepts>
#include <type_traits>
template<class T> requires (!std::movable<T>)
struct Singleton
{
};
struct Widget
{
~Widget() = default;
};
int main()
{
auto x = Singleton<Widget>{};
}
Run Code Online (Sandbox Code Playgroud)
编辑。这个版本也是如此。
#include <iostream>
#include <concepts>
#include <type_traits>
extern void fun();
template<class T> requires (!std::movable<T>)
struct Singleton
{
};
struct Widget
{
~Widget()
{
fun();
}
};
int main()
{
auto x = Singleton<Widget>{};
}
Run Code Online (Sandbox Code Playgroud)
错误信息
:23:28: 注意: 不满足约束: 替换“模板需要 !(movable) > struct Singleton [with T = Widget]”: :23:28: 从这里需要 :8:8: …