try/catch exception handling

S

Sam

Hello,

How can I use the try/catch facility in C# with OneNote 2007 methods? How
do I code the exception in catch?

Apparently MessageBox is incompatible with OneNote 2007 methods because I
get a compile error when I code using System.Windows.Forms; which is
necessary for MessageBox to work.
 
I

Ilya Koulchin

Sam said:
How can I use the try/catch facility in C# with OneNote 2007 methods? How
do I code the exception in catch?

Apparently MessageBox is incompatible with OneNote 2007 methods because I
get a compile error when I code using System.Windows.Forms; which is
necessary for MessageBox to work.

I'm not quite sure what exactly you're asking. Could you perhaps post a
small code sample demonstrating what you're trying to do and what
doesn't work?
 
S

Sam

I experimented with the following code. It compiles, but I would like to use
MessageBox.Show rather than Console.WriteLine. The other problem is that
"Exception" in the catch statement does not catch anything useful. What is
the proper exception to code in the catch statement to catch exceptions from
UpdateHierarchy (and other OneNote 2007 methods)?

try
{
OneNoteApplication.UpdateHierarchy(PCTList.ToString());
}
catch(Exception)
{
// Cannot use MessageBox. Conflict with OneNote.
Console.WriteLine("OneNote 2007 error: ",
Error.hrFileAlreadyExists);
}
 
I

Ilya Koulchin

First, please post a complete, concise, and compilable (or as close as
possible) source file (including any using directives and
namespace/class declarations) that demonstrates the problem. If a line
fails to compile, leave it in in its entirety instead of replacing it
with a comment which tells us absolutely nothing about what the error
was. Second, what are the compiler error messages and numbers that you
get? Do you get any warnings as well?
By the catch block not catching anything useful, do you mean that your
program doesn't enter the catch block when an exception is thrown, or
something else?
 
S

Sam

I am sorry I have not been able to make this clear. Maybe this time it will
make sense.

The following code gets compile error: 'Application' is an ambiguous
reference between 'Microsoft.Office.Interop.OneNote.Application' and
'System.Windows.Forms.Application' G:\My Documents\Visual Studio
2008\Projects\OneNote Create Page\OneNote Create Page\Program.cs 17 10
OneNote Create Page

From this error message it appears that MessageBox, indeed ANY Windows Forms
application, is incompatible with OneNote 2007 Addin code (any program code
that uses Microsoft.Office.Interop.OneNote.Application). Is this correct or
am I missing something here? If this compatibility is the case, is this the
way it is designed to work or is there a workaround?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using Microsoft.Office.Interop.OneNote;
using System.IO;
using System.Windows.Forms;

namespace OneNote_Create_Page
{
class Program
{
static void Main(string[] args)
{
Application OneNoteApplication = new ApplicationClass();

string PCTList = "";
string outFile = "g:\\OneNote\\OneNoteUpdate.xml";

PCTList = "<?xml version=\"1.0\"?>" +
"<one:Notebooks xmlns:eek:ne=" +
"\"http://schemas.microsoft.com/office/onenote/2007/onenote\">";
PCTList += "<one:Notebook name = \"Prioritized Composite Task
List\">";
PCTList += "<one:Section name = \"Prioritized Composite Task
List\">";
PCTList += "<one:page name = \"Prioritized Composite Task List\">";
PCTList += "<one:Title> <one:T><![CDATA[Prioritized Composite Task
List]]>" +
"</one:T></one:Title>";
PCTList += "<one:Outline><one:T><![CDATA[Item one
abcedf]]></one:T></one:Outline>";
PCTList +=
"</one:page></one:Section></one:Notebook></one:Notebooks>";

Console.WriteLine("XML: {0}", PCTList);
FileStream fs = new FileStream(outFile, FileMode.Create,
FileAccess.Write);
TextWriter tw = new StreamWriter(fs);
tw.Write(PCTList);
tw.Flush();
tw.Close();
// Update hierarchy
//OneNoteApplication.UpdateHierarchy(PCTList.ToString());

try
{
OneNoteApplication.UpdateHierarchy(PCTList.ToString());
}
catch(Exception)
{
MessageBox.Show("Ordinarily, one would code Exception.Message
here," +
" but I cannot find the appropriate exception for
UpdateHierarchy",
"An error has occurred with UpdateHierarchy",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
}
 
I

Ilya Koulchin

The error indicates that the problem is not with the message box call,
but with line 17, which reads "Application OneNoteApplication = new
ApplicationClass();". The problem is with the class type declaration,
since the type Application is defined in two of the namespaces that
you've explicitly included, specifically,
Micorosoft.Ofice.Interop.OneNote and System.Windows.Forms. The solution
is to either reference the object using the fully qualified type
(Microsoft.Office.Interop.OneNote.Application), or to not include both
of the conflicting namespaces directly into the global namespace.
 
A

Ani

You need to change the initialization line

Application OneNoteApplication = new ApplicationClass();

to

Microsoft.Office.Interop.OneNote.Application = new
Microsoft.Office.Interop.OneNote.Application()

That should fix the ambiguity. Since Windows.Forms also has an application
class, your code cannot decide which Application instance you are trying to
create.

Ani
Sam said:
I am sorry I have not been able to make this clear. Maybe this time it will
make sense.

The following code gets compile error: 'Application' is an ambiguous
reference between 'Microsoft.Office.Interop.OneNote.Application' and
'System.Windows.Forms.Application' G:\My Documents\Visual Studio
2008\Projects\OneNote Create Page\OneNote Create Page\Program.cs 17 10
OneNote Create Page

From this error message it appears that MessageBox, indeed ANY Windows Forms
application, is incompatible with OneNote 2007 Addin code (any program code
that uses Microsoft.Office.Interop.OneNote.Application). Is this correct or
am I missing something here? If this compatibility is the case, is this the
way it is designed to work or is there a workaround?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using Microsoft.Office.Interop.OneNote;
using System.IO;
using System.Windows.Forms;

namespace OneNote_Create_Page
{
class Program
{
static void Main(string[] args)
{
Application OneNoteApplication = new ApplicationClass();

string PCTList = "";
string outFile = "g:\\OneNote\\OneNoteUpdate.xml";

PCTList = "<?xml version=\"1.0\"?>" +
"<one:Notebooks xmlns:eek:ne=" +
"\"http://schemas.microsoft.com/office/onenote/2007/onenote\">";
PCTList += "<one:Notebook name = \"Prioritized Composite Task
List\">";
PCTList += "<one:Section name = \"Prioritized Composite Task
List\">";
PCTList += "<one:page name = \"Prioritized Composite Task List\">";
PCTList += "<one:Title> <one:T><![CDATA[Prioritized Composite Task
List]]>" +
"</one:T></one:Title>";
PCTList += "<one:Outline><one:T><![CDATA[Item one
abcedf]]></one:T></one:Outline>";
PCTList +=
"</one:page></one:Section></one:Notebook></one:Notebooks>";

Console.WriteLine("XML: {0}", PCTList);
FileStream fs = new FileStream(outFile, FileMode.Create,
FileAccess.Write);
TextWriter tw = new StreamWriter(fs);
tw.Write(PCTList);
tw.Flush();
tw.Close();
// Update hierarchy
//OneNoteApplication.UpdateHierarchy(PCTList.ToString());

try
{
OneNoteApplication.UpdateHierarchy(PCTList.ToString());
}
catch(Exception)
{
MessageBox.Show("Ordinarily, one would code Exception.Message
here," +
" but I cannot find the appropriate exception for
UpdateHierarchy",
"An error has occurred with UpdateHierarchy",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
}


Ilya Koulchin said:
First, please post a complete, concise, and compilable (or as close as
possible) source file (including any using directives and
namespace/class declarations) that demonstrates the problem. If a line
fails to compile, leave it in in its entirety instead of replacing it
with a comment which tells us absolutely nothing about what the error
was. Second, what are the compiler error messages and numbers that you
get? Do you get any warnings as well?
By the catch block not catching anything useful, do you mean that your
program doesn't enter the catch block when an exception is thrown, or
something else?
 
J

John Guin [msft]

You can also check out my sample code at
http://blogs.msdn.com/johnguin/archive/2008/10/07/a-calendar-planner-powertoy-for-onenote.aspx
for ways of using a messagebox to show more useful exception information.

--
Thanks,
John Guin
OneNote Test Team
http://blogs.msdn.com/johnguin


Ani said:
You need to change the initialization line

Application OneNoteApplication = new ApplicationClass();

to

Microsoft.Office.Interop.OneNote.Application = new
Microsoft.Office.Interop.OneNote.Application()

That should fix the ambiguity. Since Windows.Forms also has an application
class, your code cannot decide which Application instance you are trying to
create.

Ani
Sam said:
I am sorry I have not been able to make this clear. Maybe this time it will
make sense.

The following code gets compile error: 'Application' is an ambiguous
reference between 'Microsoft.Office.Interop.OneNote.Application' and
'System.Windows.Forms.Application' G:\My Documents\Visual Studio
2008\Projects\OneNote Create Page\OneNote Create Page\Program.cs 17 10
OneNote Create Page

From this error message it appears that MessageBox, indeed ANY Windows Forms
application, is incompatible with OneNote 2007 Addin code (any program code
that uses Microsoft.Office.Interop.OneNote.Application). Is this correct or
am I missing something here? If this compatibility is the case, is this the
way it is designed to work or is there a workaround?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using Microsoft.Office.Interop.OneNote;
using System.IO;
using System.Windows.Forms;

namespace OneNote_Create_Page
{
class Program
{
static void Main(string[] args)
{
Application OneNoteApplication = new ApplicationClass();

string PCTList = "";
string outFile = "g:\\OneNote\\OneNoteUpdate.xml";

PCTList = "<?xml version=\"1.0\"?>" +
"<one:Notebooks xmlns:eek:ne=" +
"\"http://schemas.microsoft.com/office/onenote/2007/onenote\">";
PCTList += "<one:Notebook name = \"Prioritized Composite Task
List\">";
PCTList += "<one:Section name = \"Prioritized Composite Task
List\">";
PCTList += "<one:page name = \"Prioritized Composite Task List\">";
PCTList += "<one:Title> <one:T><![CDATA[Prioritized Composite Task
List]]>" +
"</one:T></one:Title>";
PCTList += "<one:Outline><one:T><![CDATA[Item one
abcedf]]></one:T></one:Outline>";
PCTList +=
"</one:page></one:Section></one:Notebook></one:Notebooks>";

Console.WriteLine("XML: {0}", PCTList);
FileStream fs = new FileStream(outFile, FileMode.Create,
FileAccess.Write);
TextWriter tw = new StreamWriter(fs);
tw.Write(PCTList);
tw.Flush();
tw.Close();
// Update hierarchy
//OneNoteApplication.UpdateHierarchy(PCTList.ToString());

try
{
OneNoteApplication.UpdateHierarchy(PCTList.ToString());
}
catch(Exception)
{
MessageBox.Show("Ordinarily, one would code Exception.Message
here," +
" but I cannot find the appropriate exception for
UpdateHierarchy",
"An error has occurred with UpdateHierarchy",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
}


Ilya Koulchin said:
First, please post a complete, concise, and compilable (or as close as
possible) source file (including any using directives and
namespace/class declarations) that demonstrates the problem. If a line
fails to compile, leave it in in its entirety instead of replacing it
with a comment which tells us absolutely nothing about what the error
was. Second, what are the compiler error messages and numbers that you
get? Do you get any warnings as well?
By the catch block not catching anything useful, do you mean that your
program doesn't enter the catch block when an exception is thrown, or
something else?

Sam wrote:
I experimented with the following code. It compiles, but I would like to use
MessageBox.Show rather than Console.WriteLine. The other problem is that
"Exception" in the catch statement does not catch anything useful. What is
the proper exception to code in the catch statement to catch exceptions from
UpdateHierarchy (and other OneNote 2007 methods)?

try
{
OneNoteApplication.UpdateHierarchy(PCTList.ToString());
}
catch(Exception)
{
// Cannot use MessageBox. Conflict with OneNote.
Console.WriteLine("OneNote 2007 error: ",
Error.hrFileAlreadyExists);
}
:

Sam wrote:
How can I use the try/catch facility in C# with OneNote 2007 methods? How
do I code the exception in catch?

Apparently MessageBox is incompatible with OneNote 2007 methods because I
get a compile error when I code using System.Windows.Forms; which is
necessary for MessageBox to work.
I'm not quite sure what exactly you're asking. Could you perhaps post a
small code sample demonstrating what you're trying to do and what
doesn't work?
 
S

Sam

Thanks so much. That solved the compile problem.

Now for the second part of my original question: How do I code the exception
in the catch statement to retrieve the associated message text from
UpdateHierarchy? What I have coded (Exception) is obviously wrong. Please see
the bottom of this web page:
http://msdn.microsoft.com/en-us/library/ms788684.aspx. The description text
in the last column of Table 2 is what I am after.
 
S

Sam

I have done several searches of your link and cannot find any hits on
MessageBox.

John Guin said:
You can also check out my sample code at
http://blogs.msdn.com/johnguin/archive/2008/10/07/a-calendar-planner-powertoy-for-onenote.aspx
for ways of using a messagebox to show more useful exception information.

--
Thanks,
John Guin
OneNote Test Team
http://blogs.msdn.com/johnguin


Ani said:
You need to change the initialization line

Application OneNoteApplication = new ApplicationClass();

to

Microsoft.Office.Interop.OneNote.Application = new
Microsoft.Office.Interop.OneNote.Application()

That should fix the ambiguity. Since Windows.Forms also has an application
class, your code cannot decide which Application instance you are trying to
create.

Ani
Sam said:
I am sorry I have not been able to make this clear. Maybe this time it will
make sense.

The following code gets compile error: 'Application' is an ambiguous
reference between 'Microsoft.Office.Interop.OneNote.Application' and
'System.Windows.Forms.Application' G:\My Documents\Visual Studio
2008\Projects\OneNote Create Page\OneNote Create Page\Program.cs 17 10
OneNote Create Page

From this error message it appears that MessageBox, indeed ANY Windows Forms
application, is incompatible with OneNote 2007 Addin code (any program code
that uses Microsoft.Office.Interop.OneNote.Application). Is this correct or
am I missing something here? If this compatibility is the case, is this the
way it is designed to work or is there a workaround?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using Microsoft.Office.Interop.OneNote;
using System.IO;
using System.Windows.Forms;

namespace OneNote_Create_Page
{
class Program
{
static void Main(string[] args)
{
Application OneNoteApplication = new ApplicationClass();

string PCTList = "";
string outFile = "g:\\OneNote\\OneNoteUpdate.xml";

PCTList = "<?xml version=\"1.0\"?>" +
"<one:Notebooks xmlns:eek:ne=" +
"\"http://schemas.microsoft.com/office/onenote/2007/onenote\">";
PCTList += "<one:Notebook name = \"Prioritized Composite Task
List\">";
PCTList += "<one:Section name = \"Prioritized Composite Task
List\">";
PCTList += "<one:page name = \"Prioritized Composite Task List\">";
PCTList += "<one:Title> <one:T><![CDATA[Prioritized Composite Task
List]]>" +
"</one:T></one:Title>";
PCTList += "<one:Outline><one:T><![CDATA[Item one
abcedf]]></one:T></one:Outline>";
PCTList +=
"</one:page></one:Section></one:Notebook></one:Notebooks>";

Console.WriteLine("XML: {0}", PCTList);
FileStream fs = new FileStream(outFile, FileMode.Create,
FileAccess.Write);
TextWriter tw = new StreamWriter(fs);
tw.Write(PCTList);
tw.Flush();
tw.Close();
// Update hierarchy
//OneNoteApplication.UpdateHierarchy(PCTList.ToString());

try
{
OneNoteApplication.UpdateHierarchy(PCTList.ToString());
}
catch(Exception)
{
MessageBox.Show("Ordinarily, one would code Exception.Message
here," +
" but I cannot find the appropriate exception for
UpdateHierarchy",
"An error has occurred with UpdateHierarchy",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
}


:

First, please post a complete, concise, and compilable (or as close as
possible) source file (including any using directives and
namespace/class declarations) that demonstrates the problem. If a line
fails to compile, leave it in in its entirety instead of replacing it
with a comment which tells us absolutely nothing about what the error
was. Second, what are the compiler error messages and numbers that you
get? Do you get any warnings as well?
By the catch block not catching anything useful, do you mean that your
program doesn't enter the catch block when an exception is thrown, or
something else?

Sam wrote:
I experimented with the following code. It compiles, but I would like to use
MessageBox.Show rather than Console.WriteLine. The other problem is that
"Exception" in the catch statement does not catch anything useful. What is
the proper exception to code in the catch statement to catch exceptions from
UpdateHierarchy (and other OneNote 2007 methods)?

try
{
OneNoteApplication.UpdateHierarchy(PCTList.ToString());
}
catch(Exception)
{
// Cannot use MessageBox. Conflict with OneNote.
Console.WriteLine("OneNote 2007 error: ",
Error.hrFileAlreadyExists);
}
:

Sam wrote:
How can I use the try/catch facility in C# with OneNote 2007 methods? How
do I code the exception in catch?

Apparently MessageBox is incompatible with OneNote 2007 methods because I
get a compile error when I code using System.Windows.Forms; which is
necessary for MessageBox to work.
I'm not quite sure what exactly you're asking. Could you perhaps post a
small code sample demonstrating what you're trying to do and what
doesn't work?
 
I

Ilya Koulchin

Sam said:
Now for the second part of my original question: How do I code the exception
in the catch statement to retrieve the associated message text from
UpdateHierarchy? What I have coded (Exception) is obviously wrong. Please see
the bottom of this web page:
http://msdn.microsoft.com/en-us/library/ms788684.aspx. The description text
in the last column of Table 2 is what I am after.

The OneNote API only returns a single HRESULT. It is the .NET/COM
interop layer that translates that HRESULT into an exception, so the
only thing available from the exception is the error code. If you want
to translate it into a string you'll need to do it yourself.
 
J

Josh Einstein

When dealing with Office interop I always found it useful to import the
Office namespaces using aliases.

using System.Windows.Forms;
using ON = Microsoft.Office.Interop.OneNote;
using OL = Microsoft.Office.Interop.Outlook;

Application.DoEvents(); // system.windows.forms.application
var onApp = new ON.Application(); //
microsoft.office.interop.onenote.application
var olApp = new OL.Application(); //
microsoft.office.interop.outlook.application

etc...

Josh Einstein
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top