This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Public class GotBeer{ public static void main(String[ ] args) { int beerCount, pretzelCount; double pretzelsPerGlass;
System.out.println(“Enter number of pretzels:”); pretzelCount = Savit.readLineInt( ); System.out.println(“Enter number of glasses of beer:”); beerCount = Savit.readLineInt( );
if (beerCount <= 0) { System.out.printlin(“No Beer!”); System.out.println(“Go to RAT’s”); System.out.println(“Program Aborted”); System.exit(0); }
Assert.condition(beerCount>0, “No Beer!\nGo to RAT\’s\nProgramAborted”); }
System.out.prtintln(pretzelCount + “ pretzels.”); System.out.prtintln(beerCount + “ glasses of beer.”); System.out.prtintln(“You have “ + pretzelsPerGlass + “ pretzels for each glass of beer.”); }}
GotBeerGotBeer with Exceptions with Exceptions (1 of 2)(1 of 2)
Public class GotBeer{ public static void main(String[ ] args) { int beerCount, pretzelCount; double pretzelsPerGlass;
try { System.out.println(“Enter number of pretzels:”); pretzelCount = Savit.readLineInt( ); System.out.println(“Enter number of glasses of beer:”); beerCount = Savit.readLineInt( );
if (beerCount <= 0) throw new Exception(“No Beer!);
System.out.prtintln(pretzelCount + “ pretzels.”); System.out.prtintln(beerCount + “ glasses of beer.”); System.out.prtintln(“You have “ + pretzelsPerGlass + “ pretzels for each glass of beer.”);} // End of “try” block.
Catch(Exception e) { System.out.println(e.getMessage()); //”No Beer!” System.out.println(“Go to RAT’s”); System.out.println(“Program Aborted”); System.exit(0); } } } //End of “main”
An Error is used when the program should be halted.
code based on Bailey pg. 8
For example, we could modify our Ratio constructor:
public Ratio(int top, int bottom) {/* pre: bottom != 0 post: constructs a ratio equivalent to top/bottom */// Assert.pre(bottom != 0, "Denominator must not be 0");
if (bottom == 0) throw new Error(“Denominator must not be 0”);this.numerator = top;this.denominator = bottom;
An Exception is used when some special code may be used to handle the exception.
Every method that throws an exception must either catch (handle) the exception or pass the exception back to calling method using a throws clause in its method signature.
When an exception is thrown, the java interpreter looks for an exception handler.
If an exception is not caught by any handler, the exception behaves like an Error: the program is haltedhalted, a message is displayed, and the stack frames are displayed:
_exceptionOccurred: java.lang.Exception (Denominator must not be 0)java.lang.Exception: Denominator must not be 0
at Ratio.<init>(Ratio.java)at RatioTest.main(RatioTest.java)at
Semantics for Simple try-catch Semantics for Simple try-catch 11
If an expression in the try block of a statement with one catch clause and no finally clause throws an exception:
– the rest of the code in the try block is skipped.
– If the exception “matches” the catch clause, the statements in the catch block are run, followed by the rest of the method (case 3).
– If the exception does not match the catch clause, the rest of code in the method is skipped and the exception is thrown to the calling method (case 1).
Semantics for Simple try-catch Semantics for Simple try-catch 22
If an expression in the try block does not throw an exception (case 2):– The catch clause is skipped.– The rest of the method is run normally.
If you put a try-catch-finally statement around a method that may generate an exception, you can remove the throws clause in the calling method’s signature.(Why? ...because you are handling the exception in the method withthe Catch phrase.)
For example if the thrown exception class is a NumberFormatException:
public Ratio(int top, int bottom) throws NumberFormatException{
/* pre: bottom != 0 post: constructs a ratio equivalent to top/bottom */ if (bottom == 0) throw new NumberFormatException( "Denominator must not be 0"); this.numerator = top; this.denominator = bottom;}
code based on Bailey pg. 8
It can be caught by a NumberFormatException or any of its superclasses:
public Ratio divide(Ratio other) throws Exception {
/* pre: other is non-nullpre: other is not zeropost: return new fraction - the division of this and other
*/if (other == null) throw new NullPointerException();else if (other.numerator == 0) throw new ArithmeticException();else return new Ratio(this.numerator*other.denominator, this.denominator*other.numerator);} }
private static void divide(Ratio top, Ratio bottom) throws Exception
/* Compute the division of top by bottom and report the answer. If top is null then change it to an invalid ratio 1/0 to generate a NumberFormatException that won't be caught. Catch the other exceptions: bottom is zero or bottom is null. */
Ratio answer;answer = null;try { if (top == null) top = new Ratio(1, 0); //Doesn’t make sense but throws // NumberFormat Exception
answer = top.divide(bottom); } code based on Bailey pg. 9