Please help compiling nested try/catch blocks

M

Manfred Braun

Hello All,

I am trying to find out some error conditions in writing to the eventlog
remotely. To check the possible errors, I wrote a simple class which uses
nested try/catch-blocks which compiles fine until I add the

EventLog.CreateEventSource("test", "Application", "m1"}; [marked in the
code below!]

statement. The whole class is as follows [m1 is my computername]:


//EventlogSourceExistsReturnValues.cs

using System;
using System.Diagnostics;
using System.IO;

public class EventlogSourceExistsReturnValues
{
public static void Main()
{
try //computer could be unreachable, access denied ....
{
if(EventLog.SourceExists("test", "m1"))
{
Console.WriteLine("Source exist!");
}
else
{
//The computer is reachable and we got read-access ....

Console.WriteLine("Source does NOT exist, register it:");
try //mostly expected:access denied
{
//EventLog.CreateEventSource("test", "Application", "m1"};
EventLog.CreateEventSource("test", "Application", "m1"}; //THIS LINE
CAUSES COMPILER ERROR
Console.WriteLine("Source(test) registered!");
//EventLog el = new EventLog();
}
catch(Exception ex1)
{
Console.WriteLine(ex1.ToString());
Console.WriteLine(ex1.Message);
}
}
}

catch(IOException e) //Line 39
{
Console.WriteLine(e.Message);
}

catch(Exception ex)
{
Console.WriteLine(ex.ToString());
Console.WriteLine(ex.Message);
}
}
}

I cannot see any reason to get a compiler-error!!!! The compiler-output is
as follows:

EventlogSourceExistsReturnValues.cs(39,3): error CS1519: Invalid token
'catch' in class, struct, or interface member
declaration
EventlogSourceExistsReturnValues.cs(39,22): error CS1002: ; expected
EventlogSourceExistsReturnValues.cs(41,21): error CS1519: Invalid token '('
in class, struct, or interface member
declaration

It makes no difference, if I add finally() clauses and the code compiles
without error, if I remove the call to CreateEventSource. It does'nt matter,
if I specify the full namespace.

Could someone point me to my error?????? I must be blind or just cannot see
it !?!?!?

Thanks so far and
best regards,
Manfred Braun

(Private)
Mannheim
Germany

mailto:[email protected]
(Remove the anti-spam-underscore to mail me!)
 
D

Daniel O'Connell [C# MVP]

Manfred Braun said:
Hello All,

I am trying to find out some error conditions in writing to the eventlog
remotely. To check the possible errors, I wrote a simple class which uses
nested try/catch-blocks which compiles fine until I add the

EventLog.CreateEventSource("test", "Application", "m1"}; [marked in
the
code below!]

statement. The whole class is as follows [m1 is my computername]:


//EventlogSourceExistsReturnValues.cs

using System;
using System.Diagnostics;
using System.IO;

public class EventlogSourceExistsReturnValues
{
public static void Main()
{
try //computer could be unreachable, access denied ....
{
if(EventLog.SourceExists("test", "m1"))
{
Console.WriteLine("Source exist!");
}
else
{
//The computer is reachable and we got read-access ....

Console.WriteLine("Source does NOT exist, register it:");
try //mostly expected:access denied
{
//EventLog.CreateEventSource("test", "Application", "m1"};
EventLog.CreateEventSource("test", "Application", "m1"}; //THIS
LINE
CAUSES COMPILER ERROR

If you look carefully, you are closing EventLogCreateEventSource with a },
it should be a )
 
M

Manfred Braun

Hello Daniel and All,

thanks a lot, very ugly to asked about that :-(
I am working the 20th hour currently, hmm..., I should avoid this

Again, much thanks and I am rellay very sorry.....
Regards,
Manfred

Daniel O'Connell said:
Manfred Braun said:
Hello All,

I am trying to find out some error conditions in writing to the eventlog
remotely. To check the possible errors, I wrote a simple class which uses
nested try/catch-blocks which compiles fine until I add the

EventLog.CreateEventSource("test", "Application", "m1"}; [marked in
the
code below!]

statement. The whole class is as follows [m1 is my computername]:


//EventlogSourceExistsReturnValues.cs

using System;
using System.Diagnostics;
using System.IO;

public class EventlogSourceExistsReturnValues
{
public static void Main()
{
try //computer could be unreachable, access denied ....
{
if(EventLog.SourceExists("test", "m1"))
{
Console.WriteLine("Source exist!");
}
else
{
//The computer is reachable and we got read-access ....

Console.WriteLine("Source does NOT exist, register it:");
try //mostly expected:access denied
{
//EventLog.CreateEventSource("test", "Application", "m1"};
EventLog.CreateEventSource("test", "Application", "m1"}; //THIS
LINE
CAUSES COMPILER ERROR

If you look carefully, you are closing EventLogCreateEventSource with a },
it should be a )
Console.WriteLine("Source(test) registered!");
//EventLog el = new EventLog();
}
catch(Exception ex1)
{
Console.WriteLine(ex1.ToString());
Console.WriteLine(ex1.Message);
}
}
}

catch(IOException e) //Line 39
{
Console.WriteLine(e.Message);
}

catch(Exception ex)
{
Console.WriteLine(ex.ToString());
Console.WriteLine(ex.Message);
}
}
}

I cannot see any reason to get a compiler-error!!!! The compiler-output is
as follows:

EventlogSourceExistsReturnValues.cs(39,3): error CS1519: Invalid token
'catch' in class, struct, or interface member
declaration
EventlogSourceExistsReturnValues.cs(39,22): error CS1002: ; expected
EventlogSourceExistsReturnValues.cs(41,21): error CS1519: Invalid token
'('
in class, struct, or interface member
declaration

It makes no difference, if I add finally() clauses and the code compiles
without error, if I remove the call to CreateEventSource. It does'nt
matter,
if I specify the full namespace.

Could someone point me to my error?????? I must be blind or just cannot
see
it !?!?!?

Thanks so far and
best regards,
Manfred Braun

(Private)
Mannheim
Germany

mailto:[email protected]
(Remove the anti-spam-underscore to mail me!)
 
D

Daniel O'Connell [C# MVP]

Manfred Braun said:
Hello Daniel and All,

thanks a lot, very ugly to asked about that :-(
I am working the 20th hour currently, hmm..., I should avoid this

LOL its not a problem. I've found myself at 4 am and staring at a piece of
code with a simple mistake I can't find a few times as well.
Again, much thanks and I am rellay very sorry.....
Regards,
Manfred

Daniel O'Connell said:
Manfred Braun said:
Hello All,

I am trying to find out some error conditions in writing to the
eventlog
remotely. To check the possible errors, I wrote a simple class which uses
nested try/catch-blocks which compiles fine until I add the

EventLog.CreateEventSource("test", "Application", "m1"}; [marked in
the
code below!]

statement. The whole class is as follows [m1 is my computername]:


//EventlogSourceExistsReturnValues.cs

using System;
using System.Diagnostics;
using System.IO;

public class EventlogSourceExistsReturnValues
{
public static void Main()
{
try //computer could be unreachable, access denied ....
{
if(EventLog.SourceExists("test", "m1"))
{
Console.WriteLine("Source exist!");
}
else
{
//The computer is reachable and we got read-access ....

Console.WriteLine("Source does NOT exist, register it:");
try //mostly expected:access denied
{
//EventLog.CreateEventSource("test", "Application", "m1"};
EventLog.CreateEventSource("test", "Application", "m1"}; //THIS
LINE
CAUSES COMPILER ERROR

If you look carefully, you are closing EventLogCreateEventSource with
a },
it should be a )
Console.WriteLine("Source(test) registered!");
//EventLog el = new EventLog();
}
catch(Exception ex1)
{
Console.WriteLine(ex1.ToString());
Console.WriteLine(ex1.Message);
}
}
}

catch(IOException e) //Line 39
{
Console.WriteLine(e.Message);
}

catch(Exception ex)
{
Console.WriteLine(ex.ToString());
Console.WriteLine(ex.Message);
}
}
}

I cannot see any reason to get a compiler-error!!!! The compiler-output is
as follows:

EventlogSourceExistsReturnValues.cs(39,3): error CS1519: Invalid token
'catch' in class, struct, or interface member
declaration
EventlogSourceExistsReturnValues.cs(39,22): error CS1002: ; expected
EventlogSourceExistsReturnValues.cs(41,21): error CS1519: Invalid token
'('
in class, struct, or interface member
declaration

It makes no difference, if I add finally() clauses and the code
compiles
without error, if I remove the call to CreateEventSource. It does'nt
matter,
if I specify the full namespace.

Could someone point me to my error?????? I must be blind or just cannot
see
it !?!?!?

Thanks so far and
best regards,
Manfred Braun

(Private)
Mannheim
Germany

mailto:[email protected]
(Remove the anti-spam-underscore to mail me!)
 
R

Robert

Daniel said:
Hello All,

I am trying to find out some error conditions in writing to the eventlog
remotely. To check the possible errors, I wrote a simple class which uses
nested try/catch-blocks which compiles fine until I add the

EventLog.CreateEventSource("test", "Application", "m1"}; [marked in
the
code below!]

statement. The whole class is as follows [m1 is my computername]:


//EventlogSourceExistsReturnValues.cs

using System;
using System.Diagnostics;
using System.IO;

public class EventlogSourceExistsReturnValues
{
public static void Main()
{
try //computer could be unreachable, access denied ....
{
if(EventLog.SourceExists("test", "m1"))
{
Console.WriteLine("Source exist!");
}
else
{
//The computer is reachable and we got read-access ....

Console.WriteLine("Source does NOT exist, register it:");
try //mostly expected:access denied
{
//EventLog.CreateEventSource("test", "Application", "m1"};
EventLog.CreateEventSource("test", "Application", "m1"}; //THIS
LINE
CAUSES COMPILER ERROR


If you look carefully, you are closing EventLogCreateEventSource with a },
it should be a )
Console.WriteLine("Source(test) registered!");
//EventLog el = new EventLog();
}
catch(Exception ex1)
{
Console.WriteLine(ex1.ToString());
Console.WriteLine(ex1.Message);
}
}
}

catch(IOException e) //Line 39
{
Console.WriteLine(e.Message);
}

catch(Exception ex)
{
Console.WriteLine(ex.ToString());
Console.WriteLine(ex.Message);
}
}
}

I cannot see any reason to get a compiler-error!!!! The compiler-output is
as follows:

EventlogSourceExistsReturnValues.cs(39,3): error CS1519: Invalid token
'catch' in class, struct, or interface member
declaration
EventlogSourceExistsReturnValues.cs(39,22): error CS1002: ; expected
EventlogSourceExistsReturnValues.cs(41,21): error CS1519: Invalid token
'('
in class, struct, or interface member
declaration

It makes no difference, if I add finally() clauses and the code compiles
without error, if I remove the call to CreateEventSource. It does'nt
matter,
if I specify the full namespace.

Could someone point me to my error?????? I must be blind or just cannot
see
it !?!?!?

Thanks so far and
best regards,
Manfred Braun

(Private)
Mannheim
Germany

mailto:[email protected]
(Remove the anti-spam-underscore to mail me!)
 

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