Windows Vista Forums

Distinguishing Exceptions Of The Same Type
  1. #1


    urkec Guest

    Distinguishing Exceptions Of The Same Type

    I need to trap two exceptions of the same type, but I can't find a way to
    distinguish between them. So far I was only able to get the line of the
    script where exception was raised and check for variable name. Can object
    data (like for $newinstance or $newclass) be passed to the Trap section?
    This is my code:


    Trap [System.Management.ManagementException]
    {
    If($_.PSBase.InvocationInfo.Line.Contains("newinstance"))
    {"The namespace exists"}
    ElseIf($_.PSBase.InvocationInfo.Line.Contains("newclass"))
    {"The class exists"}
    Else
    {"Don't know what happened"}
    Continue;
    }


    $options = New-Object System.Management.PutOptions;
    $options.Type = [System.Management.PutType]::CreateOnly;

    $namespace = New-Object `
    System.Management.ManagementClass `
    ("root", "__Namespace", $null);



    $newinstance = $namespace.PsBase.CreateInstance();
    $newinstance.Name = "MyNamespace";
    $newinstance.Put($options);

    $newclass = New-Object `
    System.Management.ManagementClass `
    ("root\MyNamespace", [String]::Empty, $null);

    $newclass["__CLASS"] = "MyClass";
    $newclass.Put($options);


    Thanks.


    --
    urkec

      My System SpecsSystem Spec

  2. #2


    Oisin (x0n) Grehan [MVP] Guest

    Re: Distinguishing Exceptions Of The Same Type

    On Mar 9, 9:10*am, urkec <ur...@xxxxxx> wrote:

    > I need to trap two exceptions of the same type, but I can't find a way to
    > distinguish between them. So far I was only able to get the line of the
    > script where exception was raised and check for variable name. Can object
    > data (like for *$newinstance or $newclass) be passed to the Trap section?
    > This is my code:
    >
    > Trap [System.Management.ManagementException]
    > {
    > If($_.PSBase.InvocationInfo.Line.Contains("newinstance"))
    > {"The namespace exists"}
    > ElseIf($_.PSBase.InvocationInfo.Line.Contains("newclass"))
    > {"The class exists"}
    > Else
    > {"Don't know what happened"}
    > Continue;
    >
    > }
    >
    > $options = New-Object System.Management.PutOptions;
    > $options.Type = [System.Management.PutType]::CreateOnly;
    >
    > $namespace = New-Object `
    > System.Management.ManagementClass `
    > ("root", "__Namespace", $null);
    >
    > $newinstance = $namespace.PsBase.CreateInstance();
    > $newinstance.Name = "MyNamespace";
    > $newinstance.Put($options);
    >
    > $newclass = New-Object `
    > System.Management.ManagementClass `
    > ("root\MyNamespace", [String]::Empty, $null);
    >
    > $newclass["__CLASS"] = "MyClass";
    > $newclass.Put($options);
    >
    > Thanks.
    >
    > --
    > urkec
    Hi urkec,

    The trick is not to use one "trap" at the top of your script, but
    instead try to ensure that when your trap is invoked the scope leaves
    no question as to what the problem was:

    $options = New-Object System.Management.PutOptions;
    $options.Type = [System.Management.PutType]::CreateOnly;

    function TryPut ($wmiObj) {
    trap [System.Management.ManagementException] {
    write-warning "Oops! $($wmiObj.psbase.toString()) did not save
    correctly."
    break;
    }
    $wmiObj.Put($options)
    }

    $namespace = New-Object `
    System.Management.ManagementClass `
    ("root", "__Namespace", $null);

    $newinstance = $namespace.PsBase.CreateInstance();
    $newinstance.Name = "MyNamespace";

    TryPut($newinstance)

    $newclass = New-Object `
    System.Management.ManagementClass `
    ("root\MyNamespace", [String]::Empty, $null);

    $newclass["__CLASS"] = "MyClass";

    TryPut($newclass)

    Make sense?

    - Oisin

    Microsoft MVP
    http://www.nivot.org/

      My System SpecsSystem Spec

  3. #3


    urkec Guest

    Re: Distinguishing Exceptions Of The Same Type

    "Oisin (x0n) Grehan [MVP]" wrote:

    > On Mar 9, 9:10 am, urkec <ur...@xxxxxx> wrote:

    > > I need to trap two exceptions of the same type, but I can't find a way to
    > > distinguish between them. So far I was only able to get the line of the
    > > script where exception was raised and check for variable name. Can object
    > > data (like for $newinstance or $newclass) be passed to the Trap section?
    > > This is my code:
    > >
    > > Trap [System.Management.ManagementException]
    > > {
    > > If($_.PSBase.InvocationInfo.Line.Contains("newinstance"))
    > > {"The namespace exists"}
    > > ElseIf($_.PSBase.InvocationInfo.Line.Contains("newclass"))
    > > {"The class exists"}
    > > Else
    > > {"Don't know what happened"}
    > > Continue;
    > >
    > > }
    > >
    > > $options = New-Object System.Management.PutOptions;
    > > $options.Type = [System.Management.PutType]::CreateOnly;
    > >
    > > $namespace = New-Object `
    > > System.Management.ManagementClass `
    > > ("root", "__Namespace", $null);
    > >
    > > $newinstance = $namespace.PsBase.CreateInstance();
    > > $newinstance.Name = "MyNamespace";
    > > $newinstance.Put($options);
    > >
    > > $newclass = New-Object `
    > > System.Management.ManagementClass `
    > > ("root\MyNamespace", [String]::Empty, $null);
    > >
    > > $newclass["__CLASS"] = "MyClass";
    > > $newclass.Put($options);
    > >
    > > Thanks.
    > >
    > > --
    > > urkec
    >
    > Hi urkec,
    >
    > The trick is not to use one "trap" at the top of your script, but
    > instead try to ensure that when your trap is invoked the scope leaves
    > no question as to what the problem was:
    >
    > $options = New-Object System.Management.PutOptions;
    > $options.Type = [System.Management.PutType]::CreateOnly;
    >
    > function TryPut ($wmiObj) {
    > trap [System.Management.ManagementException] {
    > write-warning "Oops! $($wmiObj.psbase.toString()) did not save
    > correctly."
    > break;
    > }
    > $wmiObj.Put($options)
    > }
    >
    > $namespace = New-Object `
    > System.Management.ManagementClass `
    > ("root", "__Namespace", $null);
    >
    > $newinstance = $namespace.PsBase.CreateInstance();
    > $newinstance.Name = "MyNamespace";
    >
    > TryPut($newinstance)
    >
    > $newclass = New-Object `
    > System.Management.ManagementClass `
    > ("root\MyNamespace", [String]::Empty, $null);
    >
    > $newclass["__CLASS"] = "MyClass";
    >
    > TryPut($newclass)
    >
    > Make sense?
    >
    > - Oisin
    >
    > Microsoft MVP
    > http://www.nivot.org/
    >
    Yes, this makes sense. Thank you.

    --
    urkec

      My System SpecsSystem Spec

Distinguishing Exceptions Of The Same Type problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble distinguishing read and unread messages Dingb@ Live Mail 10 24 May 2009
memory exceptions atlantis_maniac PowerShell 0 20 Mar 2009
Display actual file type, not description of the type Doug Vista file management 3 12 Oct 2007
Unable to find type [Drawing.Image]: make sure that the assembly containing this type is loaded IT Staff PowerShell 4 16 Aug 2007
Distinguishing versions of Vista José Vista General 13 23 Jun 2006