package org.megha.blog.example.part9; /** An exception thrown when no tickets are available */ public class NoTicketsAvailableException extends Exception { } package org.megha.blog.example.part9; /** * A theatre that sells tickets. */ public class Theatre { int availableTickets; /** creates a new theatre with a fixed number of tickets */ public Theatre(int availableTickets) { this.availableTickets = availableTickets; } /** * Sells a ticket to the buyer. * * @return a unique ticket id representing the ticket sold * @throws NoTicketsAvailableException when all tickets are sold out */ public int buyTicket() throws NoTicketsAvailableException { if (availableTickets == 0) { throw new NoTicketsAvailableException(); } return availableTickets--; } public static void main(String args[]) { // create a new threatre with only 3 seats/tickets Theatre theatre = new Theatre(3); for (int i = 1; i <= 5; i++) { try { int ticketId = theatre.buyTicket(); System.out.println("Ticket #" + i + " is " + ticketId); } catch (NoTicketsAvailableException e) { System.out.println("Could not buy ticket #" + i); } } } }
Showing posts with label catch. Show all posts
Showing posts with label catch. Show all posts
Thursday, September 15, 2011
Checked Exceptions
Checked exceptions must be declared on the method and must be caught (or allowed to raise up) by the caller. Here's an example that creates a checked exception (NoTicketsAvailableException) by extending Exception. The buyTicket() method in Theatre throws this exception when there are no tickets to sell.
Unchecked Exception
An example that shows how/where an un-checked exception can be thrown. IllegalArgumentException is a sub-class of RuntimeException.
browse
package org.megha.blog.example.part8; public class MyMath { /** * Calculates the average of the numbers provided. * * @param numbers the numbers to average over. * @throws IllegalArgumentException if no numbers a specified */ public static double average(int... numbers) { // note the use of var-args above. java collapses all the arguments into an array if (numbers.length == 0) { throw new IllegalArgumentException("no numbers to average over!"); } int sum = 0; for(int i : numbers) { sum = sum + i; } return ((double) sum) / numbers.length; } public static void main(String args[]) { // averaging works over any number of arguments System.out.println(MyMath.average(1, 2, 3, 7, 9, 10)); System.out.println(MyMath.average(55, 77)); System.out.println(MyMath.average(3)); // but when it's called with no arguments, an unchecked exception is thrown. // ofcourse, we know about it already, so we have a try/catch block in place. // unchecked/runtime exceptions usually happen when some assumed condition, or // an api contract is not met by the programmer. eg passing in a null, where // null is not expected, or trying to lookup into an array beyond it's bounds. try { System.out.println(MyMath.average()); } catch (IllegalArgumentException e) { System.out.println("Exception caught: " + e); } System.out.println("still running... :)"); } }
Subscribe to:
Posts (Atom)