Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create instance of OperationAbortedException

How I can to create OperationAbortedException? This code does not work:

var ex = new OperationAbortedException("Cannot update event comment");

ERROR: Cannot access private constructor 'OperationAbortedException'.

like image 435
NieAR Avatar asked Aug 06 '26 12:08

NieAR


1 Answers

The System.Data.OperationAbortException-class (I'm assuming you mean this one, as you didn't fully qualify it in your question). has no public constructor and is sealed. These are strong hints that the framework designers didn't want you to use that exception for your own purposes. Presumably, because some inner workings of the framework would get confused if you did.

You should use a different exception for your purposes. From the message text you pass the following existing exceptions come to mind:

  • InvalidOperationException
  • NotSupportException

When selecting an existing exception from the .NET framework, make sure you not just select it by the name of the class alone. Make sure you read the respective MSDN page to understand the (original) intend for the exception. The respective documentation always starts with the words "The exception that is throw ...". If you find that text to match your intend, reuse the exception from the framework (or derive from it for finer control when eventually catching the exception).

Or create your own exception type, if you must.

like image 118
Christian.K Avatar answered Aug 09 '26 02:08

Christian.K