我正在尝试编写一个简单的 PowerShell 代码来创建注册表项,然后使用 TRY 和 CATCH 来处理/捕获可能发生的任何潜在异常。作为测试场景,如果我修改注册表路径,我预计会收到“脚本无法创建注册表项”。不幸的是,TRY/CATCH 错误处理功能对我不起作用,除了错误本身之外,控制台中没有显示任何内容。
$NetBTpath = "HKLM:\System\CurrentControlSet\Services\NetBT\Parameters"
$RegValueName = "NodeType"
Try
{
if (((Get-ItemProperty $NetBTpath).PSobject.Properties.Name -contains $RegValueName) -ne "True")
{
New-ItemProperty -Path $NetBTpath -Name "NodeType" -Value 2 -PropertyType "dword"
}
}
Catch [System.Exception]
{
Write-warning "Script failed to create the registry key"
}
Run Code Online (Sandbox Code Playgroud)
只要注册表路径正确,它就可以正常工作,但如果我将注册表文件夹 ...\NetBT\Parameters 重命名为 ...\NetBT\Parameters1,我只会看到:
Get-ItemProperty:找不到路径“HKLM:\System\CurrentControlSet\Services\NetBT\Parameters”,因为它不存在。在 C:\temp\NetBT_RegConfig222.ps1:10 char:11 + if (((Get-ItemProperty $NetBTpath).PSobject.Properties.Name -cont ... + ~~~~~~~~~~~~~ ~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (HKLM:\System\Cu...etBT\Parameters:String) [Get-ItemProperty], ItemNotFoundException + ExcellentQualifiedErrorId : PathNotFound,Microsoft。 PowerShell.Commands.GetItemPropertyCommand
New-ItemProperty:找不到路径“HKLM:\System\CurrentControlSet\Services\NetBT\Parameters”,因为它不存在。在 C:\temp\NetBT_RegConfig222.ps1:12 char:9 + New-ItemProperty …
为什么我不能在try块中分配对象变量?
如果我尝试执行此操作并清理finally块中的变量,则会出现编译器错误:"使用未分配的局部变量".这没有任何意义,因为变量是在try块之前声明的,而在finally块中我首先检查变量是否为null.
为什么以下代码无法编译?我正在检查是否dbc是null这样,它没有机会尝试用未分配的变量做某事.
例如:
DbConnection dbc;
try {
dbc = <some method call returning an open DbConnection>
// do stuff
} catch (Exception e) { // do stuff }
finally {
if (dbc != null) {
dbc.Close();
}
}
Run Code Online (Sandbox Code Playgroud) 是否有一种内置方法可以在finally块中确定您是否刚刚从catch块中出来?我知道这可以通过下面的变量轻松完成,但我很好奇是否有标准的内置方式.
boolean bException = false;
try
{
dbf = new DBFunctions();
dbf.db_run_query(query2);
dbf.rs.next();
nMonth = dbf.rs.getInt("calquarter") * 3;
nYear = dbf.rs.getInt("calyear");
}
catch (SQLException sqle)
{
out.println(sqle.toString());
bException = true;
}
finally
{
dbf.closeConnection();
if (bException == true)
return;
}
Run Code Online (Sandbox Code Playgroud)
更新:以下是closeConnection()方法的内容,它只是试图关闭所有数据库对象:
public void closeConnection()
{
if (rs != null)
{
try { rs.close(); } catch (SQLException e) { ; }
rs = null;
}
if (stmt != null)
{
try { stmt.close(); } catch (SQLException e) { ; }
stmt …Run Code Online (Sandbox Code Playgroud) 是否可以在不使用其他变量的情况下找出catch子句中捕获的异常,然后从finally子句中再次抛出它?
public void exceptionalFunction() throws Exception
{
try
{
// exception causing code
}
catch (TypeAException e)
{
// exception specific logic
}
catch (TypeBException e)
{
// exception specific logic
}
catch (TypeCException e)
{
// exception specific logic
}
finally
{
// throw the exception that was caught, if one was caught.
}
}
Run Code Online (Sandbox Code Playgroud) 为什么我们不应该使用catch块来清理代码?
我没有那么多使用错误处理技术,但我现在开始将它们用于几乎每个程序.关于时间
因此,在浏览文章/文档时,我遇到了finally块.
并且正如它所暗示的那样,无论是否存在异常,finally块都会运行(当然,如果JVM或PC被强制关闭,它将无法运行).最后,块通常用于清理代码(资源).
所以基本上,如果我的代码没有异常,那么我为什么要清理代码呢.我不应该将清理代码放在catch块而不是finally块中.
我试着寻找类似的问题,但似乎没有人问过这个问题.所以我继续前进并提出了一个问题:D
在MDN中声明,
如果finally块返回一个值,则该值将成为整个try-catch-finally生成的返回值,而不管try和catch块中的任何return语句如何:
所以我尝试执行以下代码,
function an(){
var r = try{
throw 1;
} catch(e){
console.log("Caught: %o", e);
} finally {
return 2;
}
console.log("r=%o", r);
return 3;
}
Run Code Online (Sandbox Code Playgroud)
这是行不通的.引发语法错误.
SyntaxError: syntax error
var r = try{
Run Code Online (Sandbox Code Playgroud)
这里有什么不对?
作为这方面的新手,finally在异常处理中使用该子句有什么好处.或者换句话说,当最好使用它时,最好不要使用它.
我能想到的唯一一个是关闭输入/输出流......任何其他好处??? !!
我有以下代码
public static void nocatch()
{
try
{
throw new Exception();
}
finally
{
}
}
Run Code Online (Sandbox Code Playgroud)
这给出了错误
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unhandled exception type CustomException
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么设计的catch块是可选的,当没有办法绕过没有捕获?
从finally()的角度来看,我理解这一点
finally应该至少有一个try块,catch是可选的.finally块的重点是确保无论是否抛出异常,都会清理内容.根据JLS
finally子句确保finally块在try块和任何可能执行的catch块之后执行,无论控制如何离开try块或catch块.
编辑:
通过在finally块中添加一个返回,编译器不会给出错误为什么?!
public static void nocatch()
{
try
{
throw new Exception();
}
finally
{
return; //By adding this statement, the compiler error goes away! Please let me know why
}
}
Run Code Online (Sandbox Code Playgroud) java exception-handling exception try-catch try-catch-finally
我有一个负责日志记录操作的包装器,名为OperationWrapper.它的结构简单,如下:
public void runOperation(Operation o) throws Exception{
logOperationStarted();
o.execute();
logOperationFinished();
}
Run Code Online (Sandbox Code Playgroud)
由于"o"操作可以抛出异常,因此logOperationFinished()不会始终调用该方法,因此日志记录无法正常运行.
此外,调用该runOperation()方法的各种组件处理这些异常.
我想确保logOperationFinished()始终运行,我实现了以下结构:
public void runOperation(Operation o) throws Exception{
logOperationStarted();
try{
o.execute();
}
catch(Exception e){
throw e;
}
finally{
logOperationFinished();
}
}
Run Code Online (Sandbox Code Playgroud)
现在logOperationFinished()总是运行,但我收到IntelliJ的警告:
立即重新捕获捕获的异常
报告任何捕获块,其中立即重新捕获捕获的异常,而不对其执行任何操作.这种 捕获块是不必要的或缺乏错误处理.
对我来说,似乎IntelliJ在发出此警告时没有考虑finally块.
我做错了什么还是有更好的方法来实现这个目标?
谢谢.
在finally块中,我可以告诉抛出了什么异常吗?
我理解,如果抛出异常,我们可以在finally块中进行验证.