Java Exception Example - /CS 1050 Dr Tucker Java Exception...

Info icon This preview shows pages 1–3. 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
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: //CS 1050, Dr. Tucker Java Exception Example, p. l of 3, // file ExceptD. java // Example is from Java: An Introduction to Computer // Science & Programming by Walter Savitch // readChar(), readLine() and readLineInt() are from his class // SavitchIn import java.io.*; import java.util.*; public class ExceptD { public static char readChar() throws Exception { int charAsInt = try { charAsInt } S ’1; // to keep compiler happy ystem.in.read(); catch(IOException e) { System.out.println(e.getMessage()); System.out.println(”Fatal error. System.exit(0); } return (char)charAsInt; } public static String readLine() throws Exception { char nextChar; “ String result = ""; = false; boolean done while (!done) { nextChar = if (nextChar == '\n') done = true; else if (nextChar == '\r') { } // carriage return else result = result + nextChar; } return result; readChar(); public static int readLineInt() throws Exception { String inputString = null; int number = boolean done while (!done) {try —99 = f { inputString inputString number = done = true; } 99; //value to keep compiler happy alse; = readLine(); = inputString trim(); //removes blanks (Integer.valueOf(inputString).intValue()); catch (NumberFormatException e) { System. System. System. System. System. System. } out. out. out. out. out. out. return number; } // class ExceptD COl'l println("Please try again."); println("Enter a whole number: tinued on page 2 Ending Program."); "); Spring 2009 println("Your number is not correct."); println("Your input number must be"); println("a whole nmmber written as an"); println("ordinary numeral, such as 42”); //CS 1050, Dr. Tucker Java Exception Example, p. 2 of 3, Spring 2009 public static void main (String args) throws Exception { int donutCount, milkCount; double donutsPerGlass; String inputString; try { System.out.println("Enter number of donutsz"); donutCount = readLineInt(); if (donutCount < O) { System.out.println ("Number of donuts must be >= 0"); System.out.println ("Stopping the program."); System.exit(0); } ‘ System.out.println("Enter number of glasses of milk:"); milkCount = readLineInt(); if (milkCount < l) throw new Exception("Exception: No Milk!"); donutsPerGlass = donutCount/(double)milkCount; System.out.println (donutCount + " donuts."); System.out.println (milkCount + " glasses of milk."); System.out println ("You have " + donutsPerGlass + " donuts for each glass of milk."); } catch(Exception e) { System.out.println(e.getMessage()); System.out.println("Go buy some milk."); System.out.println ("Program aborted."); System.exit(0); } } // end of main } // end of ExceptD.java // —————————————————————————————————————————————————————————————————————————————— // —————————————————————————————————————————————————————————————————————————————— // modification of the above to include a user—defined exception // two separate files are used // DonutE.java contains the user—defined exception // ExD.java contains the main program // file DonutE.java // user—defined exception // used in ExD.java public class DonutE extends Exception { public DonutE() { super("Donut Exception Thrown"); } public DonutE(String message) { super(message); } // ——————————————————————————————————————————————————————————————————————————— //file ExD.java is on the next page //CS 1050, Dr. Tucker Java Exception Example, p. 3 of 3, Spring 2009 // file DonutE.java is on the previous page // ———————————————————————————————————————————————————————————————————————— // Example is from Java: An Introduction to Computer // Science & Programming by Walter Savitch // Modified to include user—defined exception // readChar(), readLine() and readLineInt() are from his class // SavitchIn import java.io.*; import java.util.*; import DonutE *; 7/ user defined exception, maybe isn’t needed public class EXD { _ . only change from the class ExceptD is in main // see page 1 for the code before main public static void main (String args) throws DonutE, Exception { int donutCount, milkCount; double donutsPerGlass; String inputString; DonutE problem = new DonutE("Number of donuts can't be negative"); try { System.out.println("Enter number of donutsz"); donutCount = readLineInt(); if (donutCount < O) throw problem; // "Donut Exception Thrown" System.out.println("Enter number of glasses of milk:"); milkCount = readLineInt(); if (milkCount < l) throw new Exception("Exception: No Milk!"); donutsPerGlass = donutCount/(double)milkCount; System.out.println (donutCount + " donuts."); System.out.println (milkCount + " glasses of milk."); System.out.println ("You have " + donutsPerGlass + " donuts for each glass of milk."); } catch(DonutE e) { System.out.println(e.getMessage()); System.out.println ("Terminating the program."); System.exit(0); } catch(Exception e) { System.out println(e.getMessage()); System.out.println("Go buy some milk."); System.out.println ("Program aborted."); System.exit(0)f } } // end of main } // end of ExD.java ...
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