Question
try/catch
- xyz
- Hors Ligne
- Modérateur
-
Réduire
Plus d'informations
- Messages : 6311
- Remerciements reçus 69
il y a 15 ans 3 mois #8953
par xyz
Tutoriels PowerShell
Réponse de xyz sur le sujet Re:try/catch
On peut pousser le bouchon
:
[code:1]
function Throw-InnerException {
param ( [ScriptBlock]$Command)
$erroractionpreference = 'stop'
try
{
& $command
}
catch
{
Throw (New-Object $_.Exception.GetType().FullName $_.Exception.Message)
}
}
try{
Throw-InnerException {Set-Location C:\dummy}
#Throw-InnerException {Set-Location Z:\dummy}
}
catch [System.Management.Automation.ItemNotFoundException] {
Write-Host \"Répertoire introuvable !\"
}
catch [System.Management.Automation.DriveNotFoundException] {
Write-Host \"Lecteur non trouvé !\"
}
#catch on propage les autres exceptions
[/code:1]
Une erreur non bloquante peut contenir une exception :
[code:1]
$PSCmdlet.WriteError(
(New-Object System.Management.Automation.ErrorRecord(
(New-Object System.InvalidCastException ($TextMsgs.WellFormedInvalidCast -F \"Replace\",'[String]')),
\"WellFormedInvalidCast\",
\"InvalidType\",
$ParameterString
)
)
)#WriteError
[/code:1]
On peut supposer que les cmdlets respectent cette convention :
The ErrorRecord object contains the following information:
The exception that describes the error. Often, this is an exception that the cmdlet caught and converted into an error record. Every error record must contain an exception.
Du coup on ne fait que redéclencher l'exception trappée.<br><br>Message édité par: Laurent Dardenne, à: 19/03/11 19:24
[code:1]
function Throw-InnerException {
param ( [ScriptBlock]$Command)
$erroractionpreference = 'stop'
try
{
& $command
}
catch
{
Throw (New-Object $_.Exception.GetType().FullName $_.Exception.Message)
}
}
try{
Throw-InnerException {Set-Location C:\dummy}
#Throw-InnerException {Set-Location Z:\dummy}
}
catch [System.Management.Automation.ItemNotFoundException] {
Write-Host \"Répertoire introuvable !\"
}
catch [System.Management.Automation.DriveNotFoundException] {
Write-Host \"Lecteur non trouvé !\"
}
#catch on propage les autres exceptions
[/code:1]
Une erreur non bloquante peut contenir une exception :
[code:1]
$PSCmdlet.WriteError(
(New-Object System.Management.Automation.ErrorRecord(
(New-Object System.InvalidCastException ($TextMsgs.WellFormedInvalidCast -F \"Replace\",'[String]')),
\"WellFormedInvalidCast\",
\"InvalidType\",
$ParameterString
)
)
)#WriteError
[/code:1]
On peut supposer que les cmdlets respectent cette convention :
The ErrorRecord object contains the following information:
The exception that describes the error. Often, this is an exception that the cmdlet caught and converted into an error record. Every error record must contain an exception.
Du coup on ne fait que redéclencher l'exception trappée.<br><br>Message édité par: Laurent Dardenne, à: 19/03/11 19:24
Tutoriels PowerShell
Connexion ou Créer un compte pour participer à la conversation.
Temps de génération de la page : 0.033 secondes
- Vous êtes ici :
-
Accueil
-
forum
-
PowerShell
-
Entraide pour les débutants
- try/catch