S
Sean Tynan
I want to find out the best way for a method to notify calling code of
situations such as validation errors, etc. that may occur during method
execution.
e.g. say I have a method (business logic layer) that inserts a user into a
database, and returns the newly created UserID:
public function CreateUser(userName as string, userEmail as string)
dim userID as integer
'Insert user details into Users table in database
'Set userID
return userID
end function
Two possible conditions must be handled:
1. The user name already exists in the database
2. The user email already exists in the database
What is the recommended way of reporting these conditions to the calling
code?
Maybe:
1. Create custom Exceptions such as NameClashException, EmailClashException.
The CreateUser function will throw the appropriate exception if any of
these situations occur.
The calling code (presentation layer) can then catch these exceptions
and notify the user as appropriate
2. Return Status enum.
The calling code can then examine the status enum and notify the user as
appropriate.
However, another way must be found for returning the userID, e.g. pass
ByRef:
public enum CreateUserStatus
Success
NameClash
EmailClash
end enum
public function CreateUser(byRef UserID as integer, userName as string,
userEmail as string)
dim userID as integer
dim status as CreateUserStatus
'Insert user details into Users table in database
'Set status and userID
return status
end function
I have read in MS docs that option 1 is not recommended as exceptions should
only be thrown if the assumptions of the method are breached.
A clashing name or email is therefore not really an exception. Also, new
exception classes would need to be created for every possible status apart
from success.
Option 2 seems messy since it makes sense for a CreateUser method to return
just the newly created user ID.
I want to find the optimal solution for this scenario and standardise my
method calls across the application to this solution.
Any ideas?
Hope this is clear.
- Sean.
situations such as validation errors, etc. that may occur during method
execution.
e.g. say I have a method (business logic layer) that inserts a user into a
database, and returns the newly created UserID:
public function CreateUser(userName as string, userEmail as string)
dim userID as integer
'Insert user details into Users table in database
'Set userID
return userID
end function
Two possible conditions must be handled:
1. The user name already exists in the database
2. The user email already exists in the database
What is the recommended way of reporting these conditions to the calling
code?
Maybe:
1. Create custom Exceptions such as NameClashException, EmailClashException.
The CreateUser function will throw the appropriate exception if any of
these situations occur.
The calling code (presentation layer) can then catch these exceptions
and notify the user as appropriate
2. Return Status enum.
The calling code can then examine the status enum and notify the user as
appropriate.
However, another way must be found for returning the userID, e.g. pass
ByRef:
public enum CreateUserStatus
Success
NameClash
EmailClash
end enum
public function CreateUser(byRef UserID as integer, userName as string,
userEmail as string)
dim userID as integer
dim status as CreateUserStatus
'Insert user details into Users table in database
'Set status and userID
return status
end function
I have read in MS docs that option 1 is not recommended as exceptions should
only be thrown if the assumptions of the method are breached.
A clashing name or email is therefore not really an exception. Also, new
exception classes would need to be created for every possible status apart
from success.
Option 2 seems messy since it makes sense for a CreateUser method to return
just the newly created user ID.
I want to find the optimal solution for this scenario and standardise my
method calls across the application to this solution.
Any ideas?
Hope this is clear.
- Sean.