Handling Errors
- Handling Errors
- Error and/or Exception handling
- Terms
- Throw exception
- Try and Catch
- ErrorAction
- Summary
Error and/or Exception handling
Error and/or exception handling
is just part of life when it comes to writing code. We can check and validate conditions for expected behavior. When the unexpected happens, we turn to exception handling. We can easily handle exceptions generated by ready made code or you can generate your own exceptions.
Terms
Exception
An exception
is like an event that is created when normal error handling can't deal with the issue. Trying to divide a number by zero or running out of memory are examples of something that creates an exception. Sometimes the author of the code you're using creates exceptions for certain issues when they happen.
Throw and Catch
When an exception happens, we say that an exception is thrown. To handle a thrown exception, you need to catch it. If an exception is thrown and it isn't caught by something, the script stops executing.
Terminating and non-terminating errors
An exception is generally a terminating error
. A thrown exception is either be caught or it terminates the current execution. By default, a non-terminating
error is generated by Write-Error
and it adds an error to the output stream without throwing an exception . So, it is important to understand that Write-Error and other non-terminating errors do not trigger the catch in PowerShell.
Throw exception
To create our own exception event, we throw
an exception with the throw keyword.
function Start-Foo
{
throw "Bad thing happened"
}
Try and Catch
The way exception handling works in PowerShell (and of course in many other languages) is that we first try a section of code and if it throws an error, we will catch it. Here is a quick sample.
try
{
#try to do something
Start-Foo
#if all goes fine, we continue here
}
catch
{
#if a function throws exception we will continue here
Write-Output "Houston, we have a problem: script has not run properly."
}
ErrorAction
Unlike other programming languages, the PowerShell scripting language has two types of error scenarios: terminating
and non-terminating
. Both of these types of errors are bad, but by classifying them differently, we can handle them differently. This distinction is important, if you want to stop or exit a PowerShell script when it errors. Usually a PowerShell command or cmdlet does not throw exception automatically if it fails to run.
In the following example we try to run cmdlet in the folder that does not exist, see what happens. The script will fall down without going to catch-section ☹
$folder = Join-Path $HOME "DoesNotExist"
try
{
#try to run in the set folder
$files = Get-ChildItem $folder
#if all goes fine, we should continue here
Write-Host $files
}
catch
{
#if a cmdlet throws exception we will continue here
Write-Output "Houston, we have a problem: script has not run properly."
}
So, we have to add the parameter -ErrorAction Stop
for the cmdlet, that if it fails it will throw an exception.
$folder = Join-Path $HOME "DoesNotExist"
try
{
#try to run in the set folder
$files = Get-ChildItem $folder -ErrorAction Stop
#if all goes fine, we should continue here
Write-Host $files
}
catch
{
#if a cmdlet throws exception we will continue here
Write-Output "Houston, we have a problem: script has not run properly."
}