Md.1case Exception Handling

Ace your studies with our custom writing services! We've got your back for top grades and timely submissions, so you can say goodbye to the stress. Trust us to get you there!


Order a Similar Paper Order a Different Paper

Make sure you study Background materials first before moving on to case assignment.

In Background materials, we learned that there are checked and unchecked exceptions in Java. The Java compiler requires methods that throw a checked exception to have an exception handling statement, yet does not have this requirement for programs that might throw unchecked exceptions. As a good programmer however, it is good practice for us to catch unchecked exceptions even if it is not required. Next, we will view some examples.

1) Unchecked exceptions

Example A

Click here to download a program you have seen in CSC212. This program asks you to select one of the three kinds of gas, and how many gallons of gas you want to buy and then calculate how much you need to pay. Run this program, type in “three” when asked to put the amount of gas you need, and see what happens. Did the program crash? Congratulations, you just got an exception thrown at you. What kind of error message you get? Click here to find out the NumberFormatException class information. You will see thatNumberFormatException is a subclass of IllegalArgumentException which is a subclass of RuntimeException. In the background material, we learned that all exception types that ARE subclasses of RuntimeException are unchecked exceptions, which means that even there is a possibility that these exceptions would occur, and the compiler does not demand that these exceptions are handled. So your program still compiles okay but might still crash under certain conditions.  Although it is not required to write exception handling statements to deal with unchecked exceptions, it is good practice to include these statements to avoid theseproblems.

The program should be modified to include a try/catch block to handle exceptions. An if block should be added before code that might throw a NumberFormatException. This exception will be thrown when the program has attempted to convert a string to one of the numeric types, but the string does not have the appropriate format.  When users type in “three” instead of the number 3, the program is not able to convert the string into a number so a NumberFormatException is thrown.

Next, lets move on to discuss the catch statement:

catch (NumberFormatException e)
{
        JOptionPane.showMessageDialog (null, “Invaid data input, try again”);

}

You must spell NumberFormatException correctly since it is a standard java class. However, you can use whatever you like for the parameter name. In this case, I use name “e” in order to represent exception but you can use other names. Also, make sure you check JOptionPane class, and look for all the methods it has.  Specifically look for the showMessageDialog method. To further experiment with this program, modify the catch block as show below, run the program again, and see what happens.

catch (NumberFormatException e)

{
        JOptionPane.showMessageDialog (null, e.getMessage( ));

}

Example B

Download MathException.java. Run this program and see if you can crash it. Be a bad user and  do anything other than what you are supposed to do: type in double variables, strings, or even type in 0 as a denominator. Then check the error message without error checking and you will see that different kinds of exceptions might occur. Either users put in incorrect data types (type in double instead of int variable, or a string variable “three” instead of 3, etc), or an integer might divide by 0 which is an arithmetic mistake. To correct these errors, we might need multiple catch blocks to handle these errors. Download the version which has built in error checking – MathExceptionWithErrorChecking.java from https://cdad.trident.edu/Uploads/Presentations/54384MathExceptionWithErrorChecking.GIFRun the revised program again and try to break it as before.

Comments about unchecked exception:

As we can see, the compiler does not require unchecked exceptions to be caught by the program. Without exception handling, programs will still compile but might still crash from user errors or other reasons. 

2) checked exceptions

Programs that might have checked exceptions must provide exception handling statements or the compiler will not allow the program to compile. We will explore checked exceptions in module 2. 

Case assignment

Write a Java application to calculate how much federal and state tax you need to pay. The program should accomplish the following tasks: ask your name, yearly income, federal tax rate, and state tax rate, then calculate and display the amount of tax you need to pay. Your program must catch user input errors. After you are done, zip “compress” the java source file along with some screen shots of your output and submit.

Case assignment Expectations

Ability to use checked exceptions in a Java program.

Required Readings & Resources

Writerbay.net

Looking for top-notch essay writing services? We've got you covered! Connect with our writing experts today. Placing your order is easy, taking less than 5 minutes. Click below to get started.


Order a Similar Paper Order a Different Paper