C
C#2y ago
_vegabyte_

❔ Catch all the exception

Hi, I'm trying to catch all the exceptions being thrown, Is that possible?
5 Replies
_vegabyte_
_vegabyte_OP2y ago
AddNewUser.cs
public async Task<Unit> Handle(AddNewUserCommand command, CancellationToken cancellationToken)
{
var user = await _serviceManager.User.GetUserByName(command.UserName);

var department = await _serviceManager.Department.GetAllDepartmentsById(command.DepartmentId);
var userRole = await _serviceManager.UserRole.GetUserRoleById(command.UserRoleId);

if (user != null) throw new UserAlreadyExistsException(command.UserName);
if(department == null)
throw new DepartmentIsNotExists(command.DepartmentId);
if (userRole == null)
throw new UserRoleNotExist(command.UserRoleId);
var users = new Users()
{
FullName = command.FullName,
UserName = command.UserName,
Password = command.Password,
IsActive = true,
UserRoleId = command.UserRoleId,
DepartmentId = command.DepartmentId,
DateAdded = command.DateAdded,
AddedBy = "Aldrin Vega",
Reason = "Needed"
};
_serviceManager.User.AddUser(users);
await _serviceManager.SaveAsync();
return Unit.Value;

}
public async Task<Unit> Handle(AddNewUserCommand command, CancellationToken cancellationToken)
{
var user = await _serviceManager.User.GetUserByName(command.UserName);

var department = await _serviceManager.Department.GetAllDepartmentsById(command.DepartmentId);
var userRole = await _serviceManager.UserRole.GetUserRoleById(command.UserRoleId);

if (user != null) throw new UserAlreadyExistsException(command.UserName);
if(department == null)
throw new DepartmentIsNotExists(command.DepartmentId);
if (userRole == null)
throw new UserRoleNotExist(command.UserRoleId);
var users = new Users()
{
FullName = command.FullName,
UserName = command.UserName,
Password = command.Password,
IsActive = true,
UserRoleId = command.UserRoleId,
DepartmentId = command.DepartmentId,
DateAdded = command.DateAdded,
AddedBy = "Aldrin Vega",
Reason = "Needed"
};
_serviceManager.User.AddUser(users);
await _serviceManager.SaveAsync();
return Unit.Value;

}
UserController.cs
[HttpPost("AddNewUser")]
public async Task<ActionResult<QueryOrCommandResult<object>>> AddNewUser(AddNewUser.AddNewUserCommand command)
{
var response = new QueryOrCommandResult<object>();
try
{
var result = await _mediator.Send(command);
response.Success = true;
response.Data = result;
return CreatedAtRoute("GetAllUsers", result);
}
catch (Exception ex)
{
response.Messages.Add(ex.Message);
response.Success = false;
return Conflict(response);
}

}
[HttpPost("AddNewUser")]
public async Task<ActionResult<QueryOrCommandResult<object>>> AddNewUser(AddNewUser.AddNewUserCommand command)
{
var response = new QueryOrCommandResult<object>();
try
{
var result = await _mediator.Send(command);
response.Success = true;
response.Data = result;
return CreatedAtRoute("GetAllUsers", result);
}
catch (Exception ex)
{
response.Messages.Add(ex.Message);
response.Success = false;
return Conflict(response);
}

}
emileSWAAA
emileSWAAA2y ago
I suggest you to read up on this article (and find more similar) https://headsigned.com/posts/stop-using-exceptions-for-flow-control/
Esa
Esa2y ago
What do you want to do when you catch an exception?
_vegabyte_
_vegabyte_OP2y ago
If I add an User I need to input the userRoleId and departmentId. If userRoleId and DepartmentId is not exist I want to cath those two exception from useerRoleId and departmentId exceptions I'll take look on this thanks Ohhh I've read an article about this. Thank you so much!!!
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity. Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server