I am trying to understand try/catch/throw with your own exception..
This is my custom exception class:
[Serializable]
class CustomException : FormatException
{
/// <summary>
/// Just create the exception
/// </summary>
public CustomException()
: base()
{ }
/// <summary>
/// Create the exception with description
/// </summary>
/// <param name="message">Exception description</param>
public CustomException(String message)
: base(message)
{ }
/// <summary>
/// Create the exception with description and inner cause
/// </summary>
/// <param name="message">Exception description</param>
/// <param name="innerException">Exception inner cause</param>
public CustomException(String message, Exception ex)
: base(message, ex)
{
MessageBox.Show(message + ex.Message);
}
}
This is where i use it:
public static int ParseInput(string inInt)
{
try
{
int input = int.Parse(inInt);
return input;
}
catch (FormatException e)
{
throw new CustomException("Only use numbers! ", e);
}
}
Now when I run the program and but in a char the program crash it shows the MessageBox then the program stops.. and show the classic error window with this info: An unhandled exception of type 'Spelregistrering.CustomException' occurred in Spelregistrering.exe
I want the program to run after the exception like it always do with a original try/catch.. what have I not understood or missed?
EDIT: I know about the TryParse, this code I just for me to better understand custom exceptions! And your answers show that I clearly don't understand them just yet..