Question

In: Computer Science

Raising an Exception In the previous problem, you used a try/except statement to catch an exception....

Raising an Exception

In the previous problem, you used a try/except statement to catch an exception. This problem deals with the opposite situation: raising an exception in the first place.

One common situation in which you will want to raise an exception is where you need to indicate that some precondition that your code relies upon has not been met. (You may also see the assert statement used for this purpose, but we won’t cover that here.)

Write a function validate_input(string) which takes a command string in the format 'command arg1 arg2' and returns the pair ('command', [arg1, arg2]), where arg1 and arg2 have been converted to floats. If the command is not one of 'add', 'sub', 'mul', or 'div', it must raise InvalidCommand. If the arguments cannot be converted to floats, it must raise InvalidCommand.

class InvalidCommand(Exception):
pass


def validate_input(string):
"""
If string is a valid command, return its name and arguments.
If string is not a valid command, raise InvalidCommand

Valid commands:
add x y
sub x y
mul x y
div x y

Parameters:
string(str): a valid command (see above)

Return:
tuple<str, list<float>>: the command and its corresponding arguements

Precondition:
Arguments x and y must be convertable to float.
  
"""
# your code here

Solutions

Expert Solution

OUTPUT:

Exception OUTPUT for the Input "ad 2.1 3.2"


Related Solutions

Write a try/catch/finally clause to catch one or more specific Exception objects in Java.
Write a try/catch/finally clause to catch one or more specific Exception objects in Java.
How does try/catch(exception) processing help resolve run time exceptions? What is a custom exception? What are...
How does try/catch(exception) processing help resolve run time exceptions? What is a custom exception? What are it's benefits? What does it mean when we raise a custom exception?
*IN JAVA* EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define...
*IN JAVA* EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define an exception class and call it from the driver program. For this lab you will complete two parts. Part (a) of the lab implements try… catch and uses an existing exception in C# or in Java. Write a program that implements an ArrayIndexOutOfBounds error. Part (b) writes a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will enter...
(in C# please.) EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2....
(in C# please.) EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define an exception class and call it from the driver program. For this lab you will complete two parts. Part (a) of the lab implements try… catch and uses an existing exception in C# or in Java. Write a program that implements an ArrayIndexOutOfBounds error. Part (b) writes a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will...
I do not know how to: D. Apply exception handling using try-catch for System.OutOfMemoryException. the code...
I do not know how to: D. Apply exception handling using try-catch for System.OutOfMemoryException. the code i have in POWERSHELL: while($true) { $input= Read-Host "Enter the option 1-5" if ($input -eq 5) { break } #B. Create a “switch” statement that continues to prompt a user by doing each of the following activities, until a user presses key 5: switch ( $input ){ #Using a regular expression, list files within the Requirements1 folder, with the .log file extension and redirect...
Modify the following program. Add using Exception handing and polymorphism. try-catch block, Finally block in java...
Modify the following program. Add using Exception handing and polymorphism. try-catch block, Finally block in java * description of class Driver here. *these is the main class where it acts like parent class. Compiler will execute firstly static method. import java.util.Scanner; public class Driver {     public static void main (String[] args){         Scanner stdIn = new Scanner(System.in);         String user;         String computer;         char a = 'y';         do {             System.out.println("What kind of Computer would you like?");...
PYTHON 3: must use try/except for exception handling Write a function extractInt() that takes a string...
PYTHON 3: must use try/except for exception handling Write a function extractInt() that takes a string as a parameter and returns an integer constructed out of the digits that appear in the string. The digits in the integer should appear in the same order as the digits in the string. If the string does not contain any digits or an empty string is provided as a parameter, the value 0 should be return.
Java try and catch problem public void method1(){ int[] array = new int[1]; try{ array[2] =...
Java try and catch problem public void method1(){ int[] array = new int[1]; try{ array[2] = 0;} catch(ArithmeticException e){array[2] = 0} // ? finally{ return 0;}} Could you please tell me why the line with the question mark will not report an error?
In this problem you will apply the utility maximization framework to try to understand some of...
In this problem you will apply the utility maximization framework to try to understand some of the effects of the current banking crisis on consumers wellbeing. Take a two period model in which consumers utility function is U(C1,C2)=C1C2 where C1 is consumption in period 1 and C2 consumption in period 2, both retrieved in cash. The consumer is free to borrow or save in period one as the interest rate r, through the banking system. a) Write the consumer's inter-temporal...
For the previous problem, you should have found that there are significant differences among the three...
For the previous problem, you should have found that there are significant differences among the three treatments. The primary reason for the significance is that the mean for treatment I is substantially smaller than the means for the other two treatments. To create the following data, we started with the values from problem 6 and added 3 points to each score in treatment I. Recall that adding a constant causes the mean to change but has no influence on the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT