Is my solution for the following Java exception problem is correct?
-
The following problem is taken from SCJP Kathy Sierra book Propagating and Catching an Exception In this exercise you're going to create two methods that deal with exceptions. One of the methods is the main() method, which will call another method. If an exception is thrown in the other method, main() must deal with it. A finally statement will be included to indicate that the program has completed. The method that main() will call will be named reverse, and it will reverse the order of the characters in a String. If the String contains no characters, reverse will propagate an exception up to the main() method. Create a class called Propagate and a main() method, which will remain empty for now. Create a method called reverse. It takes an argument of a String and returns a String. In reverse, check if the String has a length of 0 by using theString.length() method. If the length is 0, the reverse method will throw an exception. Now include the code to reverse the order of the String. Because this isn't the main topic of this chapter, the reversal code has been provided, but feel free to try it on your own. String reverseStr = ""; for(int i=s.length()-1;i>=0;--i) { reverseStr += s.charAt(i); } return reverseStr; Now in the main() method you will attempt to call this method and deal with any potential exceptions. Additionally, you will include a finallystatement that displays when main() has finished. My solution: public class Propagate { public static void main(String[] args){ try{ System.out.println(reverse("") + "String reversed"); }catch (Exception ex){ System.out.println("the String length is 0"); }finally{ System.out.println("We have successfully completed and coded the problem"); } } public static String reverse(String input){ //Even if I include throws //exception here, it doesn't make any difference StringBuilder output = new StringBuilder(); for(int i = input.length() - 1; i >=0; --i){ output.append(input.charAt(i)); } return output.toString(); } }
-
Answer:
You're close but you have not done this part yet: In reverse, check if the String has a length of 0 by using the String.length() method. If the length is 0, the reverse method will throw an exception. Read back about the "throws" and "throw" keywords and look here http://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html
Seth Sims at Quora Visit the source
Other answers
1. Move the checkFood() method into MyException class and call it accordingly in main() method. Why because custom Exception classes are meant for grouping exceptions based on the cause and to develop meaningful exceptions based on application requirements. It should not contain any business methods. It's not meant for that purpose. Read more here : http://www.w3resource.com/java-tutorial/custom-exception.php 2. Your BadFoodException class has to override getMessage() method of exception class and in that method should return "I don't like Pasta". Because in your catch block in main(), you are printing bfee.getMessage(). So your code should look like this : public class CustomExceptionTest { public static void main(String[] args){ CustomExceptionTest test = new CustomExceptionTest(); try{ test.checkFood("Pasta"); }catch(BadFoodException bfee){ System.out.println(bfee.getMessage()); } } public void checkFood(String food) throws BadFoodException{ if(food.equalsIgnoreCase("Pasta")){ throw new BadFoodException("I don't like Pasta"); } } } class BadFoodException extends Exception{ private String message; public BadFoodException(String message){ this.message = message; } @Override public String getMessage() { return this.message; } } Output : I don't like pasta Anyway without this step, if your code is of the following format, that should work too : public class CustomExceptionTest { public static void main(String[] args){ CustomExceptionTest test = new CustomExceptionTest(); try{ test.checkFood("Pasta"); }catch(BadFoodException bfee){ System.out.println(bfee.getMessage()); } } public void checkFood(String food) throws BadFoodException{ if(food.equalsIgnoreCase("Pasta")){ throw new BadFoodException("I don't like Pasta"); } } } class BadFoodException extends Exception{ private String food; //Overloaded constructor public BadFoodException(String input){ this.food = input; } public BadFoodException(){ } } Output : null Reason : Refer point number 2 Point to be noted : You mustn't get a message like the one you mentioned, "But I got error message in IDE saying that I need to have proper constructor for the BadFoodException class". Output will just be null. But your code will compile and run successfully. Thanks for the A2A.
Radhikaa Bhaskaran
Related Q & A:
- How to handle exception in Java?Best solution by javatpoint.com
- After downloading attachments from emails i am not able to open them. How do i correct this problem?Best solution by Yahoo! Answers
- How does this solution of the subset sum problem work?Best solution by Stack Overflow
- Which of the following is the correct balance sheet presentation for current assets??Best solution by Yahoo! Answers
- Which of the following statements about this ReDox reaction are correct?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.