小编Bil*_*rry的帖子

我什么时候应该使用Write-Error vs. Throw?终止与非终止错误

在PoshCode上查看Get-WebFile脚本,http: //poshcode.org/3226 ,我注意到这个奇怪的对我来说:

$URL_Format_Error = [string]"..."
Write-Error $URL_Format_Error
return
Run Code Online (Sandbox Code Playgroud)

与以下相反,这是什么原因?

$URL_Format_Error = [string]"..."
Throw $URL_Format_Error
Run Code Online (Sandbox Code Playgroud)

甚至更好:

$URL_Format_Error = New-Object System.FormatException "..."
Throw $URL_Format_Error
Run Code Online (Sandbox Code Playgroud)

据我所知,你应该使用Write-Error来解决非终止错误,并使用Throw来终止错误,所以在我看来你不应该使用Write-Error然后使用Return.有区别吗?

error-handling powershell powershell-2.0

134
推荐指数
5
解决办法
14万
查看次数

如果某个表不存在,如果某个列在表上不存在而引发错误之前,为什么MS Sql Server会引发错误?

为什么MS Sql Server(2005和2008;其他地方未经测试)如果表中不存在列,如果表不存在则会引发错误,则会引发错误?

更具体地说,我有以下架构(高度减少以显示一切重要):

CREATE TABLE table1 (
id int identity(1,1) primary key
)
Run Code Online (Sandbox Code Playgroud)

为什么以下查询失败并显示错误Invalid column name 'iDoNotExist'.

if 1=2
begin
    print 'table that shouldn''t exist does'
    select * from [IDoNotExist]
end

if 1=2 
begin
    print 'column that shouldn''t exist does'
    select iDoNotExist from table1
end
Run Code Online (Sandbox Code Playgroud)

我希望它可能会因多个错误而失败(如果它实际编译并注意到表和列不在那里)或者没有错误(如果它忽略了if语句的内容,因为它们不会跑).我该怎么做让它运行没有错误?

PS实际查询在if语句中有这个但它没有任何区别:

exists (select * from [INFORMATION_SCHEMA].[COLUMNS] t where t.[TABLE_NAME] = 'table1' and t.[COLUMN_NAME] = 'iDoNotExist')
Run Code Online (Sandbox Code Playgroud)

sql-server sql-server-2008

4
推荐指数
1
解决办法
1149
查看次数