I try to put a exception logic which a part of a system can fall back to on unexpected behaviors.
The work should be done of a new class that inherits Exception object and extend the functionality with a new “exit” which consist of a error-signal to the user and a logging routine.
I may need to understand the use of throw better, but I though I could make it fairly transparent by i.e. this:
public SomeObject GetVersion(byte p)
{
switch ((SomeObject)p)
{
case Version.SomeType1:
...
break;
case Version.SomeType2:
...
break;
default:
throw new UnexpectedQueryException(this.someOtherObject, errorCode);
}
return (SomeObject)p;
}
I think you can see what I’m trying to do here.
I try to throw when the application can’t serve the request. The throw are meant to put the execution through the exception (which generates a adequate error code to caller). This example is a error of type “I know you gived me a 9 but only 1-8 is allowed here)”, which errorCode sends further to the UnexpectedQueryException(...).
Unfortunately the application take the throw as unhandled and closes my Thread and application won’t operate until restart. Beside this scenario i’m also use this throw in catch statements.
In my eyes, this is Very handled.
What best practice here?
I want the exception handling to be a “fall back” to activate on different scenarios (like above) so I always have an easy way to communicate an error back to the user (which mean I can send very exact info about where the exception is).
Also i, Of Course, want the application to continue working.
A part of code from the Exception logic,
public class UnexpectedQueryException: CommunicationException
{
public UnexpectedQueryException(SomeObject object, ErrorCode errorCode) : base("UnexpectedQueryException", object, errorCode)
{
.........
}
}
Which, in turn, inherits the base Exception object,
public class CommunicationException : Exception
{
..some fields..
public CommunicationException(string Message, SomeObject object, ErrorCode errorcode)
{
.....
}
public CommunicationException() : base("CommunicationException")
{ }
}
If you throw an exception in your code, you need to catch it and do something with it – if you don’t, you have not handled it.
If you throw within a
catchblock, the same thing applies. You have thrown an exception which will keep propagating till a suitable catch block has been found. If non exists, it is unhandled.You need to structure your higher level (UI) code so it catches the right types of exceptions and communicates the information you want back to the user: