Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Handle Exceptions What is an Exception? An exception is an object that contains information about an error that has occurred. When an error occurs in a method, the method throws an exception. If an exception is thrown when you’re testing a console application, some information about the exception, including its name and stack trace, is displayed at the console. A stack trace is a list of the methods that were called before the exception occurred. The list appears in reverse order, from the last method called to the first method called. All exceptions are subclasses of the Exception class. The Exception class represents the most general type of exception, while the subclasses represent increasingly specific exceptions. Murach’s Java SE 6, C5 © 2007, Mike Murach & Associates, Inc. Slide 2 Some of the classes in the Exception hierarchy Exception RuntimeException NoSuchElementException InputMismatchException IllegalArgumentException NumberFormatException ArithmeticException NullPointerException Scanner class’ next methods may throw inputMitsmatchException if the data entered is not the expected data type. For example, nextDouble method gets text data. Murach’s Java SE 6, C5 © 2007, Mike Murach & Associates, Inc. Slide 3 Example of Exception Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:840) at java.util.Scanner.next(Scanner.java:1461) at java.util.Scanner.nextDouble(Scanner.java:2387) at testinstall.Main.main(Main.java:26) Java Result: 1 BUILD SUCCESSFUL (total time: 10 seconds) Using Try / Catch Statements to Handle Exceptions The syntax for the try statement try { statements } catch(ExceptionClass exceptionName) { statements } Two ways to import the InputMismatchException class import java.util.InputMismatchException; import.java.util.*; Murach’s Java SE 6, C5 © 2007, Mike Murach & Associates, Inc. Slide 5 Try Catch: Typically work with a loop structure Scanner sc=new Scanner(System.in); try{ System.out.println("enter loan:"); double loan=sc.nextDouble(); System.out.println("enter rate in %:"); double rate=sc.nextDouble(); System.out.println("enter term in year:"); double term=sc.nextDouble(); double monPayment=(loan*rate/100/12)/(1-(Math.pow(1+rate/100/12,-12*term))); System.out.println("Monthly payment is: =" + monPayment); } catch (InputMismatchException e) { sc.next(); System.out.println("Enter numeric data only"); } System.out.println("Thank you for using payment calculator!"); Loan Calculator with Exception Handling Scanner sc=new Scanner(System.in); String flag="Y"; while (flag.equalsIgnoreCase("Y")){ try{ System.out.println("enter loan:"); double loan=sc.nextDouble(); System.out.println("enter rate in %:"); double rate=sc.nextDouble(); System.out.println("enter term in year:"); double term=sc.nextDouble(); double monPayment=(loan*rate/100/12)/(1-(Math.pow(1+rate/100/12,-12*term))); System.out.println("Monthly payment is: =" + monPayment); System.out.println("Do you want to continue? (Y/N):"); flag=sc.next(); } catch (InputMismatchException e) { sc.next(); System.out.println("Enter numeric data only"); continue; } } System.out.println("Thank you for using payment calculator!"); Methods of Exception Class • catch (InputMismatchException e) • catch (Exception e) – Catch any exceptions • System.out.println(e.getMessage()); • System.out.println(e.getStackTrace()); String Class Methods • toLowerCase() • toUpperCase() • substring(int beginIndex) – "unhappy".substring(2) returns "happy" – 0-based index • substring(int beginIndex, int endIndex) – "hamburger".substring(4, 8) returns "urge" "smiles".substring(1, 5) returns "mile" • length() • indexOf( char) Full name format: First Name + Space + Last Name Change the Full Name to proper format where the first letter of First Name and Last Name changed to uppercase and other letters in lower case Example: “david chao” -> “David Chao” Code Example public static void main(String[] args) { // TODO code application logic here Scanner sc=new Scanner(System.in); System.out.println("enter full name:"); String FullName=sc.nextLine(); int spaceIndex = FullName.indexOf(" "); String firstName=FullName.substring(0,spaceIndex); String lastName=FullName.substring(spaceIndex+1); String newFullName=proper(firstName) + " " + proper(lastName); System.out.println("Proepr full name:" + newFullName); } public static String proper(String Name){ return Name.substring(0,1).toUpperCase()+ Name.substring(1).toLowerCase(); }