Intro to Java Web-Notes_Part85

Intro to Java Web-Notes_Part85 - Home About Us When...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Home - About Us When something goes horribly wrong in your Java application, one way to handle that situation is to "throw an exception". An Exception-based object is created, the object is filled with information about the error that just occurred, and then the Exception object is thrown. Another part of the Java program is setup to receive, or catch, the thrown exception. The receiving code takes any required actions in response to the error, then processing continues normally from the place in the program where the exception was caught. There are many pre-written Java exception classes. You can use these, or create your own exception classes. Throwing an exception The following application uses a method named "upperCase" that only changes the first few characters of the word to uppercase (the second parameter indicates how many characters should be changed to uppercase). public class DemoExcl { public static void maid(String args) { // Change ls- -hree letters to uppercase String s = upperCase("word", 3); System.out.p:idtln(s); // Change ls- -wo letters to uppercase String s2 = uppe:Case("demonstrate", 2); System.out.p:idtln(s2); System.out.priqt;n( upperCase("utility", 5) ); /** The upperCase method takes a String "in" and an integer "number". The method returns the String with the first "number" of characters turned to upper case. */ public static String upperCase(String in, int number) { String out : in.substring(0, number).toUpperCase() + in.substring(number, in.length()); return out; The above application generates the following output: WORd DEmonstrate UTILIty One problem with the above upperCase method is that the caller might ask the method to change more letters than exist in the word, such as with: String s3 = upperCase("it", 9); One way for upperCasetto handle such an error is somehow return an error code if the conversion did not work. To follow such a solution, you first have to decide on the mechanism for returning the error code, and then after you’ve called the method you need to check if there was an error or not. One possible mechanism is to return a null reference if there is an error and return a valid reference to an "uppercased" String if there was no error: public class DemoExc2 { public static void main(String args) { String s : upperCase("word", 3); if (s == null) "?rror —— index too big"); System.er:.print'n( else System.ou:.print;n(s); } public static String upperCase(String in, int number) { if (number > in.length()) return null; else { String ou: : in. substring(0, number).toUpperCase() + in. substring(number, in.length()); return ou:; } The problem with such a method is that if you try to convert several words, and you check for an error after each conversion, then the error checking code overwhelms (in size) the amount of useful productive code. For example, if want you want to do is this: System.out.println( upperCase("The ', l) ), System.out.println( upperCase("orange" 4) ); System.out.println( upperCase("cat", 2) ); System.out.print’n( upperCase("was" 2) ); System.out.print’n( upperCase(' ", 3) ); 'climbing. you would have to write the following to handle all of the error checking: String s; s = upperCase("The", 1); if (s == null) System.out.print'n("?rror —— index to big"); else System.out.print;n( s ); s = upperCase("orange", 4); if (s == null) Sys-em.ou-.prin-’n("Error —— index to big"); else Sys-em.ou-.prin-’n( s ); s = upperCase("cat", 2); if (s :: null) System.out.print;n("Error —— index to big"); else System.out.println( s ); s = upperCase("was", 2) ); ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern