Application Wide Global Exception

R

Raj

Please observe the code

Exception AppErr=new Exception(); //application wide exception

{
int a = int.MaxValue;
int b = int.MaxValue - 1;

checked
{
try
{
Console.WriteLine("Before swapping a={0} b={1}", a, b);
a = a + b;
}
catch (Exception ex)
{
AppErr = ex;
}
finally
{
if (AppErr is OverflowException)
{
Console.WriteLine("Error: " + AppErr.Message);
}
else
{
Console.WriteLine("After swapping a={0} b={1}", a, b);
}
}
}
Console.Read();
}

How can I make an exception thrown in one block of code available to any
part of the application.

Whether the above is the only solution?

Thank you

Regards
Raj
 
F

Family Tree Mike

Raj said:
Please observe the code

Exception AppErr=new Exception(); //application wide exception

{
int a = int.MaxValue;
int b = int.MaxValue - 1;

checked
{
try
{
Console.WriteLine("Before swapping a={0} b={1}", a, b);
a = a + b;
}
catch (Exception ex)
{
AppErr = ex;
}
finally
{
if (AppErr is OverflowException)
{
Console.WriteLine("Error: " + AppErr.Message);
}
else
{
Console.WriteLine("After swapping a={0} b={1}", a, b);
}
}
}
Console.Read();
}

How can I make an exception thrown in one block of code available to any
part of the application.

Whether the above is the only solution?

Thank you

Regards
Raj

The only things available to "all" other parts of the application would
be public static properties of your class. I'm probably missing what
your goal is though.
 
P

Peter Duniho

Please observe the code

Exception AppErr=new Exception(); //application wide exception

Where does this statement exist? Why would you create the exception?
What are you doing with it?

Why is this brace unmatched? What is its purpose in your code example?
int a = int.MaxValue;
int b = int.MaxValue - 1;
checked
{
try
{
Console.WriteLine("Before swapping a={0} b={1}", a,
b);
a = a + b;
}
catch (Exception ex)
{
AppErr = ex;
}
finally
{
if (AppErr is OverflowException)
{
Console.WriteLine("Error: " + AppErr.Message);
}
else
{
Console.WriteLine("After swapping a={0} b={1}",
a, b);
}
}
}
Console.Read();
}

How can I make an exception thrown in one block of code available to any
part of the application.

Whether the above is the only solution?

"The above" isn't a "solution" to anything. There's not enough context
for us to see what the code actually _does_.

Taken literally, "Family Tree Mike"'s reply is the only one that makes
sense. To make anything, including an exception, "available to any part
of the application", the easiest, simplest way to do that is to store it
in a static member field somewhere, so that every part of the application
can have immediate access to it.

But the fact is, it's highly unlikely that there's any practical benefit
in making an Exception instance available to any part of the application.
Static members tend to break the "object-oriented" aspect of OOP,
flattening your object design and preventing a nice, clean encapsulation
of functionality.

Even the code you posted seems very broken. You appear to be misusing the
"catch" and "finally" clauses; if you only care about an
OverflowException, then that's the one you could catch. Or at the very
least, just inspect the exception locally in the "catch" clause, rather
than saving it in some other variable and checking it in the "finally"
clause. And if the "else" part of that "if" statement in your "finally"
clause really is supposed to be executed only when no exception occurs, it
belongs at the end of the "try" clause, not in the "finally" clause.

At best, this is what your code should look like:

int a = int.MaxValue;
int b = int.MaxValue - 1;

checked
{
try
{
Console.WriteLine("Before swapping a={0} b={1}", a, b);
a = a + b;

Console.WriteLine("After swapping a={0} b={1}", a, b);
}
catch (OverflowException ex)
{
Console.WriteLine("Error: " + AppErr.Message);
}
catch (Exception ex)
{
}
}
Console.Read();

And in reality, in context (i.e. knowing where your arithmetic came from,
based on your previous posts here) the idea of enabling exceptions for the
arithmetic operation just so you can catch it and report an error seems
wasteful to me. And if you do catch that specific exception, catching
_all_ exceptions just so you can check for that one doesn't seem correct;
you're not doing anything useful with other exceptions, but instead are
just consuming them. It's somewhat unusual that the correct
error-handling approach is to simply ignore it.

Pete
 
P

Patrice

How can I make an exception thrown in one block of code available to any
part of the application.

In addition to other replies if you are looking at creating a global
exception handler :
- see Application_Error in global.asax if this is web app.
- see AppDomain.UnhandledException if this is a window app...

Else you'll generally catch the exception locally inc ase you could do
something about it.

Making the exception globally available is really something unusual.
 

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