Question

In: Computer Science

How can I write a Python program that runs without crashing even though it has an...

How can I write a Python program that runs without crashing even though it has an error that a compiler would have caught and how can I write a Java program that crashes even after the compiler checked it. Please add comments that explain the problem, the outcome, and what this proves in both of the programs.

Solutions

Expert Solution

Python Program:

tempList= ['a', 0, 2]

for values in tempList:
try:
print("The value is", values)
r = 1/int(values)
break
except:
print("Oops!", sys.exc_info()[0], "occurred.")
print("Next value.")
print()
print("The reciprocal of", values, "is", r)

This Program runs a loop through the list , and prints the value of the reciprocal , if the element is an integer , otherwise it throws an exception. The portion that can cause an exception is placed inside the try block.The code that handles the exceptions is written in the except clause. This exception is produced by the compiler itself , in this case the exception is seen when the element is 'a' , which is a character and a reciprocal of a character variable is not possible , as there is a type mismatch. Also for the value 0 , anything divided by 0 is undefined , hence an error is thrown by compiler again.

Output:

The entry is a
Oops! <class 'ValueError'> occurred.
Next entry.

The entry is 0
Oops! <class 'ZeroDivisionError'> occured.
Next entry.

The entry is 2
The reciprocal of 2 is 0.5

Java Program:

int tempList[]={1,2,3};

for(int i=0;i<3;i++)

{

if(tempList[i]==2)

throw new ArithmeticException("Value is not valid");

else{

double r=1/tempList[i];

System.out.println("Reciprocal is "+r);

}

}

Output:

Reciprocal is 1.0

Exception in thread main java.lang.ArithmeticException:Value is not valid

Reciprocal is 0.33

This Program runs a loop through the list , and prints the value of the reciprocal , if the element is anything other than 2 , if the value is 2 it throws an Arithmetic Exception. Though the compiler does not throw an error, we throw an error explicitly , by using the keyword throw, followed by the instance exception you want to throw.

This shows that we can do exception handling in Python and throw a custom exception when needed in Java. This is basically a property of an OOP based programming language , and vice versa is also applicable.


Related Solutions

The product offers life protection, savings and even has investment component. Even though it runs for...
The product offers life protection, savings and even has investment component. Even though it runs for a specified period, life assurance companies are flexible to extend cover so that they make sure that money became available to the policyholders dependents on their death. Identify and describe the variations under this product.                            [25 marks]
I need to write this program in Python. Write a program that displays a temperature conversion...
I need to write this program in Python. Write a program that displays a temperature conversion table for degrees Celsius and degrees Fahrenheit. The tabular format of the result should include rows for all temperatures between 70 and 270 degrees Celsius that are multiples of 10 degrees Celsius (check the sample below). Include appropriate headings on your columns. The formula for converting between degrees Celsius and degrees Fahrenheit is as follow F=(9/5C) +32 Celsius Fahrenheit 70 158 80 176 90...
Write a Python program that extracts 1000 unique links from Twitter using Tweepy. How can I...
Write a Python program that extracts 1000 unique links from Twitter using Tweepy. How can I filter out all links with Twitter domains and shortened links?
Can you write this program, but in python, I just wanna see what am I doing...
Can you write this program, but in python, I just wanna see what am I doing wrong. thank you Instructions: The Gas-N-Clean Service Station sells gasoline and has a car wash. Fees for the car wash are $1.25 with a gasoline purchase of $10.00 or more and $3.00 otherwise. Three kinds of gasoline are available: regular at $2.89, plus at $3.09, and super at $3.39 per gallon. User Request: Write a program that prints a statement for a customer. Analysis:...
How can I write java program code that do reverse, replace, remove char from string without...
How can I write java program code that do reverse, replace, remove char from string without using reverse, replace, remove method. Only string method that I can use are length, concat, charAt, substring, and equals (or equalsIgnoreCase).
How can I use Python without Pandas to create a new.csv file with the headers "c1,...
How can I use Python without Pandas to create a new.csv file with the headers "c1, c2, c3, c4, c5, c6" and the first line of data "8, 5, -9, 7, 2.1, 1.7" from the original.csv file?
Write a program in python that implements quicksort, first using recursion and then without recursion.
Write a program in python that implements quicksort, first using recursion and then without recursion.
I need to write a program in python for a restaurant. The program should let the...
I need to write a program in python for a restaurant. The program should let the user enter a meal number, then it should display the meal name, meal price, and meal calories. Also, needs the appropriate output if the user enters an invalid meal number. I am supposed to use a dictionary, but my problem is it keeps giving me an error and telling me my menu is not defined. Not sure what I am doing wrong. print ("Welcome...
how can i make this program in python? Captain Øks is on his way to conquer...
how can i make this program in python? Captain Øks is on his way to conquer Mosefjell. He has a map of the area and he is in his camp at the designated place on the map. The map is divided into cells. For each cell, the height is given. Captain Øks is not happy, the landscape is full of hills, and he hates to climb both up and down. He has hired you to write a program that will...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. A: 90% - 100% B 80% - 89% C 70% - 79% D 60% - 69% F <60% The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT