Cmdlet C# finalization
So I'm working with cmdlets in C# and using a base cmdlet which derives from PSCmdlet.
Like this: Child <- Parent <- PSCmdlet.
We login to a system using methods in the Parent cmdlet. But sometimes things don't always go right with exceptions, crashes and so on. So it doesn't really logout properly.
My question is as follows:
Is there a method existing that I can implement/override in the Parent cmdlet that will run no matter what happens so that we properly can logout? Kind of like try finally.
I have checked out EndProcessing() and StopProcessing() but they aren't really up to the task in unforeseen situations.
Edit: To clarify, there are multiple cmdlets and the situation is not just about login. There are multiple exceptions that can occur based on code and user-input.
c# cmdlet pscmdlet
|
show 7 more comments
So I'm working with cmdlets in C# and using a base cmdlet which derives from PSCmdlet.
Like this: Child <- Parent <- PSCmdlet.
We login to a system using methods in the Parent cmdlet. But sometimes things don't always go right with exceptions, crashes and so on. So it doesn't really logout properly.
My question is as follows:
Is there a method existing that I can implement/override in the Parent cmdlet that will run no matter what happens so that we properly can logout? Kind of like try finally.
I have checked out EndProcessing() and StopProcessing() but they aren't really up to the task in unforeseen situations.
Edit: To clarify, there are multiple cmdlets and the situation is not just about login. There are multiple exceptions that can occur based on code and user-input.
c# cmdlet pscmdlet
Are the errors/exceptions preventable (code-wise, not end-user)? If so, then spend the time to correct them. Are the errors/exceptions due to user input? Then use try/catch/finally. You mentioned something like try/finally. Why does use of try/finally not work for your situation?
– Symon
Nov 21 '18 at 14:21
In some cases of the cmdlets we throw exceptions by design, e.g UnauthorizedException. We still want the exceptions that occur during execution of cmdlet.
– YanerTavuz
Nov 21 '18 at 14:25
Do these purposeful exceptions stop execution ?
– Symon
Nov 21 '18 at 14:30
Yes, they do stop the execution.
– YanerTavuz
Nov 21 '18 at 14:33
1
You could rethrow a caught exception depending on your needs/desires. Shouldn't stop you from using try-catch blocks...
– elgonzo
Nov 21 '18 at 14:47
|
show 7 more comments
So I'm working with cmdlets in C# and using a base cmdlet which derives from PSCmdlet.
Like this: Child <- Parent <- PSCmdlet.
We login to a system using methods in the Parent cmdlet. But sometimes things don't always go right with exceptions, crashes and so on. So it doesn't really logout properly.
My question is as follows:
Is there a method existing that I can implement/override in the Parent cmdlet that will run no matter what happens so that we properly can logout? Kind of like try finally.
I have checked out EndProcessing() and StopProcessing() but they aren't really up to the task in unforeseen situations.
Edit: To clarify, there are multiple cmdlets and the situation is not just about login. There are multiple exceptions that can occur based on code and user-input.
c# cmdlet pscmdlet
So I'm working with cmdlets in C# and using a base cmdlet which derives from PSCmdlet.
Like this: Child <- Parent <- PSCmdlet.
We login to a system using methods in the Parent cmdlet. But sometimes things don't always go right with exceptions, crashes and so on. So it doesn't really logout properly.
My question is as follows:
Is there a method existing that I can implement/override in the Parent cmdlet that will run no matter what happens so that we properly can logout? Kind of like try finally.
I have checked out EndProcessing() and StopProcessing() but they aren't really up to the task in unforeseen situations.
Edit: To clarify, there are multiple cmdlets and the situation is not just about login. There are multiple exceptions that can occur based on code and user-input.
c# cmdlet pscmdlet
c# cmdlet pscmdlet
edited Nov 21 '18 at 16:43
D Manokhin
625220
625220
asked Nov 21 '18 at 14:18
YanerTavuzYanerTavuz
215
215
Are the errors/exceptions preventable (code-wise, not end-user)? If so, then spend the time to correct them. Are the errors/exceptions due to user input? Then use try/catch/finally. You mentioned something like try/finally. Why does use of try/finally not work for your situation?
– Symon
Nov 21 '18 at 14:21
In some cases of the cmdlets we throw exceptions by design, e.g UnauthorizedException. We still want the exceptions that occur during execution of cmdlet.
– YanerTavuz
Nov 21 '18 at 14:25
Do these purposeful exceptions stop execution ?
– Symon
Nov 21 '18 at 14:30
Yes, they do stop the execution.
– YanerTavuz
Nov 21 '18 at 14:33
1
You could rethrow a caught exception depending on your needs/desires. Shouldn't stop you from using try-catch blocks...
– elgonzo
Nov 21 '18 at 14:47
|
show 7 more comments
Are the errors/exceptions preventable (code-wise, not end-user)? If so, then spend the time to correct them. Are the errors/exceptions due to user input? Then use try/catch/finally. You mentioned something like try/finally. Why does use of try/finally not work for your situation?
– Symon
Nov 21 '18 at 14:21
In some cases of the cmdlets we throw exceptions by design, e.g UnauthorizedException. We still want the exceptions that occur during execution of cmdlet.
– YanerTavuz
Nov 21 '18 at 14:25
Do these purposeful exceptions stop execution ?
– Symon
Nov 21 '18 at 14:30
Yes, they do stop the execution.
– YanerTavuz
Nov 21 '18 at 14:33
1
You could rethrow a caught exception depending on your needs/desires. Shouldn't stop you from using try-catch blocks...
– elgonzo
Nov 21 '18 at 14:47
Are the errors/exceptions preventable (code-wise, not end-user)? If so, then spend the time to correct them. Are the errors/exceptions due to user input? Then use try/catch/finally. You mentioned something like try/finally. Why does use of try/finally not work for your situation?
– Symon
Nov 21 '18 at 14:21
Are the errors/exceptions preventable (code-wise, not end-user)? If so, then spend the time to correct them. Are the errors/exceptions due to user input? Then use try/catch/finally. You mentioned something like try/finally. Why does use of try/finally not work for your situation?
– Symon
Nov 21 '18 at 14:21
In some cases of the cmdlets we throw exceptions by design, e.g UnauthorizedException. We still want the exceptions that occur during execution of cmdlet.
– YanerTavuz
Nov 21 '18 at 14:25
In some cases of the cmdlets we throw exceptions by design, e.g UnauthorizedException. We still want the exceptions that occur during execution of cmdlet.
– YanerTavuz
Nov 21 '18 at 14:25
Do these purposeful exceptions stop execution ?
– Symon
Nov 21 '18 at 14:30
Do these purposeful exceptions stop execution ?
– Symon
Nov 21 '18 at 14:30
Yes, they do stop the execution.
– YanerTavuz
Nov 21 '18 at 14:33
Yes, they do stop the execution.
– YanerTavuz
Nov 21 '18 at 14:33
1
1
You could rethrow a caught exception depending on your needs/desires. Shouldn't stop you from using try-catch blocks...
– elgonzo
Nov 21 '18 at 14:47
You could rethrow a caught exception depending on your needs/desires. Shouldn't stop you from using try-catch blocks...
– elgonzo
Nov 21 '18 at 14:47
|
show 7 more comments
2 Answers
2
active
oldest
votes
The solution for my problem, as @PetSerAl suggested, was to implement IDisposable in the Parent class.
Using Dispose() I could then add whatever methods needed to finalize the cmdlet no matter what happens.
add a comment |
This sounds like a good case to use Try-catch
:
try
{
UserLogin();
OtherMethod();
}
catch(UnauthorizedException au)
{
Console.WriteLine("Unauthorized user... shutting down");
//Closing Code...
Environment.Exit(0);
}
catch(OtherException oe)
{
//Closing Code...
Environment.Exit(0);
}
...
Where you would (if not already) throw the UnauthorizedException()
exception within UserLogin()
or which ever method you are using to test credentials.
This concept can also be applied to the other areas of code that you are expecting exceptions to be thrown. If the exceptions are because of code not working properly, Try-Catch
should not be used to suppress that error as it should be corrected. Exceptions/errors based off of User-Input could be wrapped with Try-Catch
.
I clarified in my question that it's not just UnauthorizedException, but I see what you mean.
– YanerTavuz
Nov 21 '18 at 15:07
You would be able tocatch
filter as needed. WhereOtherException
should obviously be the name of the actual exception. Updated Answer
– Symon
Nov 21 '18 at 15:10
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414096%2fcmdlet-c-sharp-finalization%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The solution for my problem, as @PetSerAl suggested, was to implement IDisposable in the Parent class.
Using Dispose() I could then add whatever methods needed to finalize the cmdlet no matter what happens.
add a comment |
The solution for my problem, as @PetSerAl suggested, was to implement IDisposable in the Parent class.
Using Dispose() I could then add whatever methods needed to finalize the cmdlet no matter what happens.
add a comment |
The solution for my problem, as @PetSerAl suggested, was to implement IDisposable in the Parent class.
Using Dispose() I could then add whatever methods needed to finalize the cmdlet no matter what happens.
The solution for my problem, as @PetSerAl suggested, was to implement IDisposable in the Parent class.
Using Dispose() I could then add whatever methods needed to finalize the cmdlet no matter what happens.
answered Nov 22 '18 at 7:23
YanerTavuzYanerTavuz
215
215
add a comment |
add a comment |
This sounds like a good case to use Try-catch
:
try
{
UserLogin();
OtherMethod();
}
catch(UnauthorizedException au)
{
Console.WriteLine("Unauthorized user... shutting down");
//Closing Code...
Environment.Exit(0);
}
catch(OtherException oe)
{
//Closing Code...
Environment.Exit(0);
}
...
Where you would (if not already) throw the UnauthorizedException()
exception within UserLogin()
or which ever method you are using to test credentials.
This concept can also be applied to the other areas of code that you are expecting exceptions to be thrown. If the exceptions are because of code not working properly, Try-Catch
should not be used to suppress that error as it should be corrected. Exceptions/errors based off of User-Input could be wrapped with Try-Catch
.
I clarified in my question that it's not just UnauthorizedException, but I see what you mean.
– YanerTavuz
Nov 21 '18 at 15:07
You would be able tocatch
filter as needed. WhereOtherException
should obviously be the name of the actual exception. Updated Answer
– Symon
Nov 21 '18 at 15:10
add a comment |
This sounds like a good case to use Try-catch
:
try
{
UserLogin();
OtherMethod();
}
catch(UnauthorizedException au)
{
Console.WriteLine("Unauthorized user... shutting down");
//Closing Code...
Environment.Exit(0);
}
catch(OtherException oe)
{
//Closing Code...
Environment.Exit(0);
}
...
Where you would (if not already) throw the UnauthorizedException()
exception within UserLogin()
or which ever method you are using to test credentials.
This concept can also be applied to the other areas of code that you are expecting exceptions to be thrown. If the exceptions are because of code not working properly, Try-Catch
should not be used to suppress that error as it should be corrected. Exceptions/errors based off of User-Input could be wrapped with Try-Catch
.
I clarified in my question that it's not just UnauthorizedException, but I see what you mean.
– YanerTavuz
Nov 21 '18 at 15:07
You would be able tocatch
filter as needed. WhereOtherException
should obviously be the name of the actual exception. Updated Answer
– Symon
Nov 21 '18 at 15:10
add a comment |
This sounds like a good case to use Try-catch
:
try
{
UserLogin();
OtherMethod();
}
catch(UnauthorizedException au)
{
Console.WriteLine("Unauthorized user... shutting down");
//Closing Code...
Environment.Exit(0);
}
catch(OtherException oe)
{
//Closing Code...
Environment.Exit(0);
}
...
Where you would (if not already) throw the UnauthorizedException()
exception within UserLogin()
or which ever method you are using to test credentials.
This concept can also be applied to the other areas of code that you are expecting exceptions to be thrown. If the exceptions are because of code not working properly, Try-Catch
should not be used to suppress that error as it should be corrected. Exceptions/errors based off of User-Input could be wrapped with Try-Catch
.
This sounds like a good case to use Try-catch
:
try
{
UserLogin();
OtherMethod();
}
catch(UnauthorizedException au)
{
Console.WriteLine("Unauthorized user... shutting down");
//Closing Code...
Environment.Exit(0);
}
catch(OtherException oe)
{
//Closing Code...
Environment.Exit(0);
}
...
Where you would (if not already) throw the UnauthorizedException()
exception within UserLogin()
or which ever method you are using to test credentials.
This concept can also be applied to the other areas of code that you are expecting exceptions to be thrown. If the exceptions are because of code not working properly, Try-Catch
should not be used to suppress that error as it should be corrected. Exceptions/errors based off of User-Input could be wrapped with Try-Catch
.
edited Nov 21 '18 at 15:09
answered Nov 21 '18 at 14:50
SymonSymon
760321
760321
I clarified in my question that it's not just UnauthorizedException, but I see what you mean.
– YanerTavuz
Nov 21 '18 at 15:07
You would be able tocatch
filter as needed. WhereOtherException
should obviously be the name of the actual exception. Updated Answer
– Symon
Nov 21 '18 at 15:10
add a comment |
I clarified in my question that it's not just UnauthorizedException, but I see what you mean.
– YanerTavuz
Nov 21 '18 at 15:07
You would be able tocatch
filter as needed. WhereOtherException
should obviously be the name of the actual exception. Updated Answer
– Symon
Nov 21 '18 at 15:10
I clarified in my question that it's not just UnauthorizedException, but I see what you mean.
– YanerTavuz
Nov 21 '18 at 15:07
I clarified in my question that it's not just UnauthorizedException, but I see what you mean.
– YanerTavuz
Nov 21 '18 at 15:07
You would be able to
catch
filter as needed. Where OtherException
should obviously be the name of the actual exception. Updated Answer– Symon
Nov 21 '18 at 15:10
You would be able to
catch
filter as needed. Where OtherException
should obviously be the name of the actual exception. Updated Answer– Symon
Nov 21 '18 at 15:10
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414096%2fcmdlet-c-sharp-finalization%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are the errors/exceptions preventable (code-wise, not end-user)? If so, then spend the time to correct them. Are the errors/exceptions due to user input? Then use try/catch/finally. You mentioned something like try/finally. Why does use of try/finally not work for your situation?
– Symon
Nov 21 '18 at 14:21
In some cases of the cmdlets we throw exceptions by design, e.g UnauthorizedException. We still want the exceptions that occur during execution of cmdlet.
– YanerTavuz
Nov 21 '18 at 14:25
Do these purposeful exceptions stop execution ?
– Symon
Nov 21 '18 at 14:30
Yes, they do stop the execution.
– YanerTavuz
Nov 21 '18 at 14:33
1
You could rethrow a caught exception depending on your needs/desires. Shouldn't stop you from using try-catch blocks...
– elgonzo
Nov 21 '18 at 14:47