You are creating a custom exception class named ProductDoesNotExistException so that custom exception
messages are displayed in a new application when the product specified by users is unavailable.
This custom exception class will take the ProductID as an argument to its constructor and expose this value through the ProductID.
You are now in the process of creating a method named UpdateProduct.
This method will be used to generate and manage the ProductDoesNotExistException exception if the ProductID variable contains the value 0.
You need to ensure that use the appropriate code for the UpdateProduct method.
What should you do?
A.
Make use of the following code:
public void UpdateProduct (){
try{
if (ProductID == 0)
throw new ProductDoesNotExistException (ProductID);
}
catch (ProductDoesNotExistException ex){
MessageBox.Show (“There is no Product” + ex. ProductID);
}
}
B.
Make use of the following code:
public void UpdateProduct (){
try{
if (ProductID == 0)
throw new Exception (“Invalid ProductID”);
}
catch (ProductDoesNotExistException ex){
MessageBox.Show (ex.Message);
}
}
C.
Make use of the following code:
public void UpdateProduct (){
if (ProductID == 0)
throw new ProductDoesNotExistException (ProductID);
}
D.
Make use of the following code:
public void UpdateProduct (){
if (ProductID == 0)
throw new Exception (“Invalid ProductID”);
}
Explanation:
This code verifies the value of the ProductID variable by using the if statement. If the ProductID variable contains a value of 0, this code generates an exception of type ProductDoesNotExistException. To explicitly generate an exception, you are required to use the throw statement. The exception generated by using the throw statement can be handled by the try…catch block. This code generates the custom exception by calling the constructor of the custom exception class named ProductDoesNotExistException. The constructor argument is the ProductID attached to the ProductDoesNotExistException object. This code then handles the custom exception named ProductDoesNotExistException by using a catch block, which handles exceptions by using a variable named ex of the type ProductDoesNotExistException. This code displays the “There is no Product” error message by using the MessageBox.Show method and concatenating the ex.ProductID to it.
Incorrect Answers:
B: You should not use the code that generates an exception of the type Exception and handles the exception of the type ProductDoesNotExistException in the catch block. This code is incorrect because you are required to generate a custom exception named ProductDoesNotExistException.
C, D: You should not use the codes that do not use a try…catch block because the application an unhandled exception.
I choose A
What i don’t understood is in reality how you are now not really much more well-appreciated
than you may be right now. You are so intelligent.
You already know thus significantly in terms of this matter,
made me in my opinion imagine it from so many numerous angles.
Its like men and women aren’t involved except it’s one thing
to do with Girl gaga! Your individual stuffs great.
All the time deal with it up!