从函数返回数据时的最佳做法是什么.返回Null或空对象更好吗?为什么要一个人做另一个呢?
考虑一下:
public UserEntity GetUserById(Guid userId)
{
//Imagine some code here to access database.....
//Check if data was returned and return a null if none found
if (!DataExists)
return null;
//Should I be doing this here instead?
//return new UserEntity();
else
return existingUserEntity;
}
Run Code Online (Sandbox Code Playgroud)
让我们假设在这个程序中有效的案例,数据库中没有该GUID的用户信息.我想在这种情况下抛出异常是不合适的?此外,我的印象是异常处理可能会损害性能.
如何检查SqlDataReader对象中是否存在列?在我的数据访问层中,我创建了一个方法,为多个存储过程调用构建相同的对象.其中一个存储过程具有另一个列,其他存储过程不使用该列.我想修改方法以适应每个场景.
我的应用程序是用C#编写的.
我经常看到/听到人们说异常应该很少使用,但永远不解释原因.虽然这可能是真的,但理由通常是一种愚蠢:"它被称为例外的原因",对我来说,这似乎是一种不应被一位受人尊敬的程序员/工程师接受的解释.
可以使用异常来解决一系列问题.为什么将它们用于控制流程是不明智的?对它们的使用方式保持格外保守的理念是什么?语义?性能?复杂?美学?惯例?
我之前已经看过一些关于性能的分析,但是在与某些系统相关且与其他系统无关的水平上.
同样,我不一定不同意他们应该在特殊情况下得救,但我想知道共识的理由是什么(如果这样的事情存在的话).
预告:伙计们,这个问题不是关于如何实施重试政策.这是关于正确完成TPL数据流块.
这个问题主要是我之前在ITargetBlock中重试策略的问题的延续.这个问题的答案是@ svick使用TransformBlock(源)和TransformManyBlock(目标)的智能解决方案.剩下的唯一问题是以正确的方式完成此块:等待所有重试首先完成,然后完成目标块.这是我最终得到的结果(它只是一个片段,不要过多关注非线程安全retries集):
var retries = new HashSet<RetryingMessage<TInput>>();
TransformManyBlock<RetryableMessage<TInput>, TOutput> target = null;
target = new TransformManyBlock<RetryableMessage<TInput>, TOutput>(
async message =>
{
try
{
var result = new[] { await transform(message.Data) };
retries.Remove(message);
return result;
}
catch (Exception ex)
{
message.Exceptions.Add(ex);
if (message.RetriesRemaining == 0)
{
if (failureHandler != null)
failureHandler(message.Exceptions);
retries.Remove(message);
}
else
{
retries.Add(message);
message.RetriesRemaining--;
Task.Delay(retryDelay)
.ContinueWith(_ => target.Post(message));
}
return null;
}
}, dataflowBlockOptions);
source.LinkTo(target);
source.Completion.ContinueWith(async …Run Code Online (Sandbox Code Playgroud) 我想从一个闭包返回,就像在循环中使用break语句一样.
例如:
largeListOfElements.each{ element->
if(element == specificElement){
// do some work
return // but this will only leave this iteration and start the next
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的if语句中,我想停止遍历列表并离开闭包以避免不必要的迭代.
我已经看到了一个解决方案,在闭包内抛出一个异常并被抓到外面,但我不太喜欢那个解决方案.
除了更改代码以避免这种算法之外,还有其他解决方案吗?
现在我的页面看起来像这样:
if($_GET['something'] == 'somevalue')
{
$output .= 'somecode';
// make a DB query, fetch a row
//...
$row = $stmt->Fetch(PDO::ASSOC);
if($row != null)
{
$output .= 'morecode';
if(somethingIsOK())
{
$output .= 'yet more page output';
}
else
{
$error = 'something is most definitely not OK.';
}
}
else
{
$error = 'the row does not exist.';
}
}
else
{
$error = 'something is not a valid value';
}
if($error == '') // no error
{
//display $output on …Run Code Online (Sandbox Code Playgroud) llvm/clang被认为是很好的C++代码库.我想知道为什么C++异常根本不用于它们?
使用类似池的内容管理内存,并使用返回值和C中的代码报告错误.它们甚至将operator new包装为placement new,返回错误,而没有内存时也不例外.
您是否知道为什么llvm哲学在大多数书籍推荐使用时不会使用C++异常?
我是Grails/Groovy的新手,我正在尝试在xml文件中找到一个节点; 我已经想出如何迭代所有这些,但我想在找到目标节点时退出循环.我读过它而不是使用"each",使用"find",但我看到的查找示例是条件.现在我的逻辑将迭代整个文件而不退出.代码如下:
records.children().each {domain ->
println "domain_name: " + domain.@domain_name
if (domain.@domain_name == targetDomain) {
println "target domain matched: " + domain.@domain_name
domain.children().each {misc_field ->
println "field_name: " + misc_field.@field_name
println "field_type: " + misc_field.@field_type
println "field_value: " + misc_field
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用cx_Oracle连接到Oracle实例并执行一些DDL语句:
db = None
try:
db = cx_Oracle.connect('username', 'password', 'hostname:port/SERVICENAME')
#print(db.version)
except cx_Oracle.DatabaseError as e:
error, = e.args
if error.code == 1017:
print('Please check your credentials.')
# sys.exit()?
else:
print('Database connection error: %s'.format(e))
cursor = db.cursor()
try:
cursor.execute(ddl_statements)
except cx_Oracle.DatabaseError as e:
error, = e.args
if error.code == 955:
print('Table already exists')
if error.code == 1031:
print("Insufficient privileges - are you sure you're using the owner account?")
print(error.code)
print(error.message)
print(error.context)
cursor.close()
db.commit()
db.close()
Run Code Online (Sandbox Code Playgroud)
但是,我不太确定这里的异常处理的最佳设计是什么.
首先,我db在try块中创建对象,以捕获任何连接错误.
但是,如果它无法连接,那么db将不再存在 …
如果用户将从键盘输入值,我必须设置默认值.以下是用户可以输入值的代码:
input = int(raw_input("Enter the inputs : "))
Run Code Online (Sandbox Code Playgroud)
这里的值将input在输入值后分配给变量并点击'Enter',是否有任何方法,如果我们不输入值并直接点击'Enter'键,变量将直接指定默认值,如input = 0.025.