Reputation: 209
I am trying to understand how to use a custom exception in the right way.
I have used try/catch many times but never understod when to use your own class off exceptions. I have read and watch some off the many tutorials out there, but I can't get my head around this.
This is my CustomException
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 innerException)
{
}
}
This is where I try to use it:
/// <summary>
/// Checks if parse works
/// </summary>
/// <returns></returns>
public static int ParseInput(string inInt)
{
try
{
int input = int.Parse(inInt);
return input;
}
catch (CustomException)
{
throw new CustomException();
}
catch (Exception ex)
{
MessageBox.Show("Use only numbers! " + ex.Message);
return -1;
}
}
Now what do I do wrong? Becuse the program crash att this line int input = int.Parse(inInt);
, it never comes to my custom exception? If I do use the classic Exception
class it all works.
Upvotes: 0
Views: 954
Reputation: 119
Well, it happens because int.Parse(int )
in your case raises FormatException
and know nothing (John_Snow_face.jpg) about your custom exception.
You can use TryParse
method :
int number;
bool result = Int32.TryParse(value, out number);
if(!result)
throw new CustomException("oops");
Upvotes: 1
Reputation: 56697
In your code you are only throwing your type of exception when you caught it before, which will never happen, as the system will not throw it.
Normally you would do something like this.
try
{
// do something
if (some condition)
throw new MyCustomException() ;
}
catch (SomeOtherException e)
{
// Handle other exceptions
}
In your case, the system will throw a FormatException
, but not your own exception class, as it doesn't know about it. As your exception class is more specific as the FormatException
, the catch block will not be called. However, it should work like this:
public static int ParseInput(string inInt)
{
try
{
int input = int.Parse(inInt);
return input;
}
catch (FormatException e)
{
throw new CustomException("Format error!", e);
}
catch (Exception ex)
{
MessageBox.Show("Use only numbers! " + ex.Message);
return -1;
}
}
In addition, your code has one flaw: When any Exception
is caught, a message will be displayed to the user and -1 will be the return value (is -1 not a valid input...?). When a FormatException
is caught, error handling is left to the called by re-throwing the exception - why?
Please remember that exceptions don't "fall through":
try
{
}
catch (FormatException e1)
{
// This will throw an exception, but it will **not** be caught here,
// but control goes back to the caller
throw new Exception();
}
catch (Exception e2)
{
// This block will not get called when an exception occurs in
// above handler. It will only get called when an exception other
// than FormatException occurs.
}
Upvotes: 4
Reputation: 27962
Let me start my answer with a question: How do you think you could catch a given exception when it does not occur in the code?
The code here:
int input = int.Parse(inInt);
throws FormatException
in case of an error, but not your very own CustomException
.
You have to throw it within your own code in order to catch it:
try
{
…
// the execution of this code needs to lead to throwing CustomException…
throw new CustomException();
…
}
// … for a handler of CustomException to make any sense whatsoever
catch (CustomException e)
{
… handle here …
}
Upvotes: 2
Reputation: 108
CustomException you defined is a more specific class than the base FormatException (this is what inheritance is about). You cannot catch a more generic exception with a more specific one. Only the other way.
Upvotes: 8