praetorean.nomad

hi,

need a little help... i'm writing a piece of code which contains a catch() block...
my stumbling block is here is how do i determine the exception...
it's like this...

try
{
[code here]
}
catch (Exception oEx)
{
[exception code here...]
}

inside the catch block.. how do i determine what type of exception was caught
i'm using the Exception() coz i have no way of knowing how to determine the exact type of exception...

Thanks...


Re: Visual C# General determining exceptions...

Sibin

Can you specify the exact code you are using to be more clear




Re: Visual C# General determining exceptions...

Zamial

Well you can't really.

Although you can but it's not nice.

Exception is a .Net generic exception and is block that will be executed when no other suitable catch is found.

You should, if you believe an exception can occur that you want to catch, catch it yourself like

try

{

}

catch (SqlException ex1)

{

}

catch (NullReferenceException ex2)

{

}

catch (FileNotFoundException ex3)

{

}

catch (Exception ex4)

{

//All other exceptions

}

Some methods show you which exceptions they can throw.





Re: Visual C# General determining exceptions...

Peter Ritchie

If you don't know what exception you're catching when you write the code then how do you know you can do anything with the exception

If you know what exception to compare oEx to (like ArgumentException for example), the generally accepted practice is to add a catch block for that exception:

Code Snippet

try

{

//...

}

catch(ArgumentException argumentException)

{

// Code that does something when ArgumentException occurs

}






Re: Visual C# General determining exceptions...

praetorean.nomad

in reference to zamial's advise, i'm trying out his approach:

Code Snippet
function doSomething()
{
try [1]
{
//do codes here///

try [2]
{
//do codes here to invoke a service...
}
catch(FaultException oFaultEx)
{
//handle exception for FaultException
}
catch(EndpointNotFoundException oEndPointEx)
{
//handle exception for EndpointNotFoundException
}
catch (ServiceActivationException oServiceEx)
{
//handle exception for ServiceActivationException
}

}
catch(Exception oEx)
{
//handle general exception here..
}
}



i'm actually working on an exception for a SOA based application... & what i'm trying to do here are a follows:

marked
[1] : this try-catch block is intended to be generic... providing facility to catch exceptions in the application in general. i understand that i could use ApplicationException on the catch clause, however... i can decide to do so at a later time...

marked
[2] : this try-catch block is intended to catch exceptions specific to exceptions generated when invoking a request to the web service (wcf). i have opted to limit only to catching the following exception:
-
FaultException: for the exception that occurs within the methods in the service...
-
EndpointNotFoundException: for the exception that occurs if the application has not been configured with an
endpoint.
-
ServiceActivationException: for the exception that occurs if the service is not exposed to be consumed by the
application.



Any share for a cent or two would be appreciatted...
Thanks again...





Re: Visual C# General determining exceptions...

Chuck the Code Monkey

Catching the general Exception is almost always a bad idea. Generally you only want to catch the exceptions that you can handle and do something about. If you can't actually do anything with it then the exception simply gets swallowed never delt with (potentially wreaking havok later on). If the exception is not caught then it will simply propogate up to some place it can be caught and dealt with. If it doesn't get caught then it becomes an unhandled exception and crashes the program, in which case it's usually some drastic problem that can't be recovered from anyways so the application should crash. For instance, say an OutOfMemoryException happens to get caught in the catch(Exception ex), what would you actually do with it Nothing really, it's most always a sign that something has gone seriously wrong elsewhere. Unfortunately you may never actually find the problem if you never have the exception come up because it got swalowed unintentionally by a generic catch.