Powershell 4.0: Problem with error handling inside Eventhandlers - Powershell has stopped working

Hallo!
Please see the attached sample code. This code simply show a small WPF-Dialog.
Test 1 (showing the problem)
Simply run the program. You will see Powershell crashing "Powershell has stopped working".
The cause of this is the devision by 0 inside the Eventhandler.
We would normally expect, that the exception should be handled by the trap.
Test 2 (showing normal operation)
Simply remove the comment in line 28 ($i=1/0).
This activates an exception outside the Eventhandler. Now the Trap will be executed normally.
--> The exception can be handled by the Powershell Devloper.
Can anybody help? Why does the Error management via Trap not work inside Eventhandlers?
Thanks for some assistance and ideas!
Regards
Heike
cls
Set-StrictMode -Version 4.0
$ErrorActionPreference = "stop"
Add-Type -Assemblyname PresentationFramework
# -- Eventhandler - will be fired during form load -----------------------------------------------------
$FormLoaded = {
Write-Host "EventHandler is running..."
# Problem:
# All exceptions will crash Powershell.
# We would normally expect that the Global Trap will be executed!
# This problem occurs inside of Eventhandlers only!
# TEST 1 --> Create an Exception (Division by 0) --> Powershell crashes: "Powershell_ISE has stopped working"
$i=1/0
# -- End of Eventhandler----------------------------------------------------------------------------------
# -- Start of Demo-Programm ------------------------------------
Write-Host "Start Demo"
# -- TEST 2 ---
# Write-Host "Test 2: Testing Global Trap"
# Create an Exception (Division by 0) --> Powershell does not crash --> Global Trap will be executed --> OK
# $i=1/0
# XAML-Code for WPF-Dialog
[xml]$xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Test - " Height="370" Width="657" ResizeMode="NoResize" WindowStyle="ThreeDBorderWindow" WindowStartupLocation="CenterScreen" ShowInTaskbar="False" >
<Grid Margin="0,0,0,-4">
<Label Name="CustomerName" Content="Hello World" HorizontalAlignment="Left" Margin="10,2,0,0" VerticalAlignment="Top" Width="596"/>
</Grid>
</Window>
$Form1=[Windows.Markup.XamlReader]::Load((New-Object System.Xml.XmlNodeReader $xaml)) # Load form
Write-Host "Register Event"
$Form1.Add_Loaded($FormLoaded)
Write-Host "Starting ShowDialog"
$Form1.ShowDialog() > $null
Write-Host "End of program"
# Global Trap - all exceptions should be handled here
trap{
Write-host "Global Trap is running..."
Write-host "End of trap - program will be stopped"

It works fine if you don't try to execute the code while loading and if you use a better exception handling method.
Add-Type -Assemblyname PresentationFramework
[xml]$xaml=@'
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Test - " Height="370" Width="657" ResizeMode="NoResize" WindowStyle="ThreeDBorderWindow" WindowStartupLocation="CenterScreen" ShowInTaskbar="False" >
<Grid Margin="0,0,0,-4">
<Label Name="CustomerName" Content="Hello World" HorizontalAlignment="Left" Margin="10,2,0,0" VerticalAlignment="Top" Width="596"/>
</Grid>
</Window>
$Form1=[Windows.Markup.XamlReader]::Load((New-Object System.Xml.XmlNodeReader $xaml)) # Load form
$Form1.Add_Loaded({
Write-Host "EventHandler is running..."
Try{
$i=1/0
Catch{
Write-Host "$_"
$Form1.ShowDialog()
¯\_(ツ)_/¯

Similar Messages

  • When i load i tunes, it comes up with error message stating "i tunes has stopped working.A problem caused the program to stop working correctly. Windows will clode the program and notify you if a solution is available". I have no idea to fix

    New laptop, load itunes to find a error message that states, "i tunes has stopped working. A problem caused the program to stop working correctly. Windows will close the program and notify you if a solution is available". It has me stuffed on such a problem that should be easy to load. Please help!!!!

    Hey there Rodney274,
    It sounds like you are getting an error from iTunes when you launch it. I would try the troubleshooting in this article named:
    iTunes for Windows Vista, Windows 7, or Windows 8: Fix unexpected quits or launch issues
    http://support.apple.com/kb/ts1717
    Start with troubleshooting for 3rd party plug ins section, then the rest of the article if needed:
    Start iTunes in Safe Mode
    Open iTunes in Safe Mode to isolate any interference from plug-ins or scripts not manufactured by Apple.
    Hold down Shift–Control while opening iTunes. You should see a dialog that says "iTunes is running in safe mode" before iTunes finishes starting up.
    Click Continue.
    See if the issue you're experiencing persists in Safe Mode.
    If you have the same issue while iTunes is in Safe Mode, proceed to the "Create a new user account" section. If you don't experience the same issue, follow these steps to remove third-party plug-ins.
    Thank you for using Apple Support Communities.
    Take care,
    Sterling

  • Itunes comes up with error message on opening "itunes has stopped working..

    Help
    i have installed itunes onto my vista laptop and it stops working when opened, it comes up with an error message "itunes has stopped working microft is checking for a solution" and then closed itunes. I have followed apples advise and unistalled and installed it a few times, removed all unecessary files etc etc.

    "itunes has stopped working microft is checking for a solution"
    Fingers crossed, your Vista problem history may give us more information on what is going on.
    Go into your Problem Reports and Solutions Control Panel, click on the Problem History, and look for iTunes problem reports that occur at the same time as those error messages. Open the problem reports. Do they all say the same thing? If so, paste the contents of a typical problem report into a reply here.

  • Error when opening itunes : iTunes has stopped working ''A problem caused the program to stop working correctly''. When I repaired damaged files the problem still exists and also after uninstalling and redownloading and installing itunes. I have windows 8

    Error when opening iTunes : iTunes has stopped working '' A problem caused the program to stop working correctly''.
    When I repaired damaged files the problem still exists and also after uninstalling, redownloading and reinstalling iTunes.
    My pc is working with windos 8.
    Is there a solution?

    Hey there Rodney274,
    It sounds like you are getting an error from iTunes when you launch it. I would try the troubleshooting in this article named:
    iTunes for Windows Vista, Windows 7, or Windows 8: Fix unexpected quits or launch issues
    http://support.apple.com/kb/ts1717
    Start with troubleshooting for 3rd party plug ins section, then the rest of the article if needed:
    Start iTunes in Safe Mode
    Open iTunes in Safe Mode to isolate any interference from plug-ins or scripts not manufactured by Apple.
    Hold down Shift–Control while opening iTunes. You should see a dialog that says "iTunes is running in safe mode" before iTunes finishes starting up.
    Click Continue.
    See if the issue you're experiencing persists in Safe Mode.
    If you have the same issue while iTunes is in Safe Mode, proceed to the "Create a new user account" section. If you don't experience the same issue, follow these steps to remove third-party plug-ins.
    Thank you for using Apple Support Communities.
    Take care,
    Sterling

  • TS4062 I keep getting the error message "apple outlook DAV has stopped working"

    My phone and I pad all of the sudden just stopped syncing with my desktop.  I have had my phone for over a year when 4s first came out and everything was syncing fine.  Then all the sudden it stopped.  I keep getting the error message "apple outlook DAV has stopped working".  I have rebooted, checked for duplicates, signed in and out of the cloud and still nothing.  Has anyone had this problem and know of a good way to handle it.  Also, now my phone and Ipad don't sync with the cloud either.  Please help.

    Windows 7 Professional.  I meant that when I open a word doc and want to turn it into a PDF, i get the same error message.  In the mean time I paid the $40.00 and spoke to Adobe and after 40 minutes, they re-installed Reader X instead of XI so now it works but I am not overly thrilled that I had to pay for it.

  • Error "Licensing for this product has stopped working" when you launch Acrobat 9

    Hi,
    I'm part of service desk and my company has several thousand users of Acrobat. There has been strange issues reported since upgrading from 7.0.5 to 9.1 and a lot of them are uhappy. The latest issue is users started to receive the following error,
    Error "Licensing for this product has stopped working" when you launch Acrobat 9 or Adobe Creative Suite 4 products. The link http://kb2.adobe.com/cps/405/kb405970.html#ErrorCodes has solution, but i tried everything given for 147-178, and nothing works. The users are highly frustrated. Any other possibilities? one collegue claims running simple reg cleaner could solve this issue.

    Wow,
    I went through a "font crisis" on my MacBook about two weeks ago, and I reinstalled Snow Leopard, then restored everything from Time Machine backup. Everything but Office 2008 and CS4 was able to run from the restore, because there were preferences for both that wouldn't copy back to my Library (permissions).
    I reinstalled both from the "original media" (dmg's)  with my Time Machine and 2 other external HDs connected and mounted. I had no problems with either. In fact since I was re-installing from dmg's stored on my Time Machine drive, I had to have it connected to do this. Installing Office and CS4 from the dmg's took all of twenty minutes for both with no errors at all.
    I'm glad you got everything taken care of, but anectdotal evidence tells me CS4 can be reinstalled with external drives connected and mounted.

  • Error Message: Unfortunately Xperia Home has stopped working

    I have had my Z3 for nearly 3 weeks now and until this morning all was working well - loved the phone! 
    2 things happened this morning (may be connected).
    On first trying to use the phone (from it being asleep overnight) the screen froze and it would do nothing.  I left it a few minutes (so it went back to sleep again) and all appeared then to be fine.  I did a restart.
    A little later (no more than 90mins) I found the phone to be showing an error message - Unfortunately Xperia Home has stopped working - (OK).   On clicking OK, message appeared again, and again and again. I tried rebooting the phone and hoped this would clear the error. It didn't.  I was, however, by a very round about route able to get on-line. 
    Swipe down to quick settings
    Settings
    Change home screen to simple
    Error message still came up but would go once I clicked OK. I managed to get online and did a search for the error - found this thread:
    http://talk.sonymobile.com/t5/Xperia-J/Unfortunately-Xperia-Home-has-stopped-Working/td-p/720607 
    I had a system update that had downloaded and was ready to install, so before trying to clear all the data on xperia home (as suggested in the thread - Apps - All - Xperia Home - clear data) I ran the update.  This didn't clear the error - Xperia home has stopped working. 
    So, I followed the instructions on the thread above ie, all apps, xperia home, clear data. (It doesn't actually clear all the data, your accounts, passwords etc are all left intact. However, it does clear your home screens of all settings and personalisation (although it didn't change my background).  
    Touch wood, the phone now seems to be OK (having reset my home screens.
    Having been an xperia user for a number of years (I loved my Z and had no problems with it and actually delayed upgrading) I am extremely disappointed that this flagship phone should start giving error messages such as this, particularly after only 3 weeks of use.  I only hope that this is the last time I see this message!
    At least am tech savvy enough to have been able to circumvent the error, get online, find a solution quickly and sort the problem out - all done in the space of 40mins on the way to work!!   
    Has anyone else had this problem?
    Does Sony have any answers as to why this has occurred?
    Hope this all makes sense!!

    Repair your phone with SUS 
    Update Service (SUS)
    PC Companion (PCC)
    Bridge (for Mac)
    Alternatives on How to backup Xperias
    http://talk.sonymobile.com/thread/36355
    "I'd rather be hated for who I am, than loved for who I am not." Kurt Cobain (1967-1994)

  • When I click on any of my movie or TV shows in Itunes I immediately get an error message that says Itunes has stopped working and closes the program. I can sync the movies and shows to my phone okay but I can't watch on Itunes. Can anyone help?

    When I click on any of my movie or TV shows in Itunes I immediately get an error message that says Itunes has stopped working and closes the program. I can sync the movies and shows to my phone okay but I can't watch on Itunes. Can anyone help?

    Let's try the following user tip with that one:
    iTunes for Windows 10.7.0.21: "iTunes has stopped working" error messages when playing videos, video podcasts, movies and TV shows

  • New Adobe Photoshop elements 11-can not share pictures. I do use AOL email. Get error of "Elements 11 Organ. has stopped working,  I have looked into sharing tab and my only option is Adobe email settings.  I do have outlook set up to work on computer run

    New Adobe Photoshop elements 11-can not share pictures. I do use AOL email. Get error of "Elements 11 Organ. has stopped working,  I have looked into sharing tab and my only option is Adobe email settings.  I do have outlook set up to work on computer running windows 8.1  Please help, Mainly use to share pictures.  Thanks!

    One thing puzzles me:
    RedClayFarmer wrote:
    I then found one suggestion that the problem might involve permissions. The suggestion was to right click PhotoshopElementsOrganizer.exe in its installation folder (which on my computer is at at D:\Photo\Elements 11 Organizer) and run Organizer as an administrator. This also failed.
    I don't understand why running the exe from the installation folder would have worked.
    I would have simply tried to run that exe from its real location :
    Sorry, I can't help you more about permissions...

  • I am using ITUNES 11 on Win 7 64 bit.  I can download podcasts to my IPOD touch just fine but when I click on the ITUNES store I get an error message that stays "ITUNES has stopped working".  How do I trouble shoot or fix this? Thanks

    I am using ITUNES 11 on Win 7 64 bit.  I can download podcasts to my IPOD touch just fine but when I click on the ITUNES store I get an error message that stays "ITUNES has stopped working".  How do I trouble shoot or fix this? Thanks

    Try gn_id's suggestion from the following post:
    Re: iTunes 11 crashes when opening the store - no error message

  • I have just upgraded from Windows XP to Windows 7 then reinstalled the CS4 I had previously paid for (including the unique code number).  Now I get an error message that says "Licencing has stopped working".

    I have just upgraded from Windows XP to Windows 7 and then reinstalled CS4 (purchased a couple of years back and paid for) then input the unique code number.  Now I get an error msg that says "Licencing has stopped working".  What do I need to do?

    Hello Carole,
    Please try the steps mentioned in the link below:
    Error "Licensing has stopped working" | Windows
    Thanks
    Alisha

  • Error message - photoshop elements editor has stopped working

    Hi - I've installed photoshop elements 8 twice, and I still get the error message "photoshop elements editor has stopped working" .  I can't seem to get beyond this point.  My system is Windows Vista.  Any help is appreciated.  Thanks

    I suddenly started getting the same error message and was reaching my wits end in trying to find a solution.
    This may sound bizarre, and you may not have the same situation as I found, but check your system/Windows date setting.
    Somehow, the year on my system had become 2096. I corrected it, and bingo! PSE 8 Editor works again.

  • Have a error message "canvas database sync has stopped working"?

    Have a error message "canvas database sync has stopped working"? My computer is a HP touchsmart 300, windows 7 64 bit OS? Need help in fixing it.

    Does it work in a new file? If it does, see Remove minor corruption by exporting. If not, see Replace Your Preferences

  • When I try to load a film, I get an error message "Premier Eliments 12 has stopped working"

    when I try to load a film, I get an error message "Premier Elements 12 has stopped working"

    You'll need to get the specs for these videos and set up your Premiere Elements project accordingly. (MOV is just the package for the files. It tells you nothing of the video specs.)
    Open the video files in the free download G Spot or Media Info and post to this forum the dimensions, frame rate and audio and video codecs listed.

  • HT1349 I have icloud on my pc with windows vista.  Recently, upon startup I receive an error that says applephotostream.exe has stopped working.  Windows will close this problem . . . any suggestions.  I tried to download the control panel again. no luck

    I have icloud on my pc with windows vista.  recently, upon startup I get a message that says applephotostream.exe has stopped working.  Windows will close this program  . . . I tried re-installing the icloud control panel, etc.  to no avail.  any leads?

    Hi, I have the same problem. Has anyone come forward with any advice on fixing the problem?

Maybe you are looking for