Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating my own exceptions c#

Tags:

Following examples in my C# book and I came across a book example that doesn't work in Visual Studio. It deals with creating your own exceptions, this one in particular is to stop you from taking the square root of a negative number. But when I create the NegativeNumberException by using "throw new" I get an error that says "The type or namespace name 'NegativeNumberException' could not be found (are you missing a using directive or an assembly reference?)"

How can I create my own exceptions if this isn't the right way? Maybe my book is outdated? Here's the code:

class SquareRootTest {     static void Main(string[] args)     {         bool continueLoop = true;          do         {             try             {                 Console.Write("Enter a value to calculate the sqrt of: ");                 double inputValue = Convert.ToDouble(Console.ReadLine());                 double result = SquareRoot(inputValue);                  Console.WriteLine("The sqrt of {0} is {1:F6)\n", inputValue, result);                 continueLoop = false;             }             catch (FormatException formatException)             {                 Console.WriteLine("\n" + formatException.Message);                 Console.WriteLine("Enter a double value, doofus.\n");             }             catch (NegativeNumberException negativeNumberException)             {                 Console.WriteLine("\n" + negativeNumberException.Message);                 Console.WriteLine("Enter a non-negative value, doofus.\n");             }         } while (continueLoop);     }//end main     public static double SquareRoot(double value)     {         if (value < 0)             throw new NegativeNumberException(                 "Square root of negative number not permitted.");         else             return Math.Sqrt(value);     } } 
like image 761
Evan Lemmons Avatar asked Dec 05 '13 04:12

Evan Lemmons


People also ask

How can I create my own exception in C plus?

C++ exception handling is built upon three keywords: try, catch, and throw. throw − A program throws an exception when a problem shows up. This is done using a throw keyword. catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem.

Can I throw my own exception?

You can create your own exceptions in Java. All exceptions must be a child of Throwable. If you want to write a checked exception that is automatically enforced by the Handle or Declare Rule, you need to extend the Exception class. If you want to write a runtime exception, you need to extend the RuntimeException class.

Why create your own exceptions?

Custom exceptions provide you the flexibility to add attributes and methods that are not part of a standard Java exception. These can store additional information, like an application-specific error code, or provide utility methods that can be used to handle or present the exception to a user.


1 Answers

Exception is just a class like many other classes in .Net. There're, IMHO, two tricky things with user defined exceptions:

  1. Inherit your exception class from Exception, not from obsolete ApplicationException
  2. User defined exception should have many constructors - 4 in the typical case

Something like that:

public class NegativeNumberException: Exception {   /// <summary>   /// Just create the exception   /// </summary>   public NegativeNumberException()     : base() {   }    /// <summary>   /// Create the exception with description   /// </summary>   /// <param name="message">Exception description</param>   public NegativeNumberException(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 NegativeNumberException(String message, Exception innerException)     : base(message, innerException) {   }    /// <summary>   /// Create the exception from serialized data.   /// Usual scenario is when exception is occured somewhere on the remote workstation   /// and we have to re-create/re-throw the exception on the local machine   /// </summary>   /// <param name="info">Serialization info</param>   /// <param name="context">Serialization context</param>   protected NegativeNumberException(SerializationInfo info, StreamingContext context)     : base(info, context) {   } } 
like image 95
Dmitry Bychenko Avatar answered Jan 05 '23 08:01

Dmitry Bychenko