创建和抛出新异常 [英] Creating and throwing new exception

查看:55
本文介绍了创建和抛出新异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 PowerShell 中创建和抛出新异常?

How I can create and throw a new exception in PowerShell?

我想针对特定错误做不同的事情.

I want to do different things for a specific error.

推荐答案

要调用 FileNotFoundException 等特定异常,请使用此格式

To call a specific exception such as FileNotFoundException use this format

if (-not (Test-Path $file)) 
{
    throw [System.IO.FileNotFoundException] "$file not found."
}

要抛出一般异常,请使用后跟字符串的 throw 命令.

To throw a general exception use the throw command followed by a string.

throw "Error trying to do a task"

在 catch 中使用时,您可以提供有关触发错误的其他信息

When used inside a catch, you can provide additional information about what triggered the error

这篇关于创建和抛出新异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆