le said is true. It looks like starting a transaction in wrong mode is considered an exception that cannot be recovered. That is, if a programmer (only a programmer will start a transaction in wrong mode from code, isn't it?) starts a transaction in a mode, he/she should never use the transaction in other mode.
It is intereding to know that the catch... clause actually does catches the exception and the code in the catch...clause is executed (well, if the code is still executeable in the context of exception has happed). It is just that even the catch...clause cactches the exception, AutoCAD still buble up the exception to upper level and then even it is caught and handled again, it is still bubles up and eventually shut down AutoCAD. I made a bit change to the code by adding a try..catch.. to wrap up the using... block, so that there are 2 levels of try..cacth... inside and outside using...block, like this:
[CommandMethod("MyTran")]
public static void TestTransaction()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityResult res = ed.GetEntity("\nPick entity:");
if (res.Status != PromptStatus.OK) return;
ObjectId id = res.ObjectId;
try
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
try
{
Entity ent = (Entity)trans.GetObject(id, OpenMode.ForRead);
ent.ColorIndex = 1;
trans.Commit();
}
catch
{
ed.WriteMessage("\nError!!!");
}
}
}
catch (System.Exception ex)
{
ed.WriteMessage("\nError: " + ex.Message);
}
}
If stepping through the code, when ent.ColorIndex=1 is executed, one can see:
1. AutoCAD pops up "AutoCAD Error Aborting" message box with message" Internal Error.....eNotOpenForWrite"
2. AutoCAD then pops up Another message box to offer an option to save changes of current drawing. User can click either yes or no.
3. the catch...clause inside the using... block catches the exception, code inside the catch clause runs.
4. after the inner cacth...clause executed, the catch... clause outside using... block also catches the exception.
5. If there is another try...catch... oudside, the exception would be caught there again.
6. No matter how many try...catch... one wrap up the code, this exception keeps bubling up, even it is catch and handled by each catch...clause, and eventaully shut down AutoCAD.