Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Exceptions Exceptions Exceptions One system unit raises or throws an exception, and another unit catches the exception which processes (handles) it. Throw an exception-Interrupt the normal processing of a program to raise an exception that must be handled or rethrown by surrounding block or code. You throw an exception when you've detected a problem that the implementation shouldn't have to resolve or cannot easily resolve.  A throw is usually found in a conditional statement, i.e., only throw an exception if a particular condition holds. Catch an exception-Code that is executed to handle a thrown exception is said to "catch the exception". Catches are part of the code that may anticipate problems in another class object that the object may throw back. Unchecked Exception This is an exception of the RunTimeException class, it does not have to be explicitly handled by the method within which it might be raised. Catching and Handling an Exception try { month = Integer.parseInt(dataFile.readLine()); day = Integer.parseInt(dataFile.readLine()); year = Integer.parseInt(dataFile.readLine()); } catch (Exception readExcp) { outFile.println("There was trouble reading numeric form of date"); outFile.println("Exception: "+readExcp.getMessage()); System.exit(); } We caught all possible exceptions above. We don't know if there was problems with the input file or if the data was malformed.  We can catch different, specific exceptions as in: try { month = Integer.parseInt(dataFile.readLine()); day = Integer.parseInt(dataFile.readLine()); year = Integer.parseInt(dataFile.readLine()); } catch (IOException readExcp) { outFile.println("There was trouble reading from input file"); outFile.println("Exception: "+readExcp.getMessage()); System.exit(); } catch (NumberFormatException fmtExcp) { outFile.println("There was trouble reading numeric form of date"); outFile.println("Exception: "+fmtExcp.getMessage()); System.exit(); } catch (Exception genExcp) //this general catch must follow specific { outFile.println("There are unresolved problems retrieving date"); outFile.println("Exception: "+genExcp.getMessage()); System.exit(); } Defining and Throwing Our Own Exceptions Exceptions are simply objects.  You throw and catch an object; it's best that object is from the Exception class hierarchy within which you can define your own. Let's start by downloading DateExcepLab.java and UseDateExcep.java . Add sufficient comments, including: your name, the date, purpose, along with comments highlighting the "main" segments of code, to each file you edit. At the end of your test driver, include comments with sample input and the associated output. Compile and run UseDateExcep.java Now, we will modify the code to handle "exceptional situations."  In this case, we will focus on whether the input month or input year are not correct.  We could also handle some of the other input problems covered above.  Note that the DateExcepLab.java already has a mechanism to handle invalid days, in a method called getDate, but that method isn't called initially. Create the DateOutOfBoundsException class. It extends the Exception class, and typically you define a default constructor that simply invokes its parent default constructor, as well as one that includes a user's message.: public class DateOutOfBoundsException extends Exception { public DateOutOfBoundsException() { super(); } public DateOutOfBoundsException(String message) { super(message); } } Next, update DateExcepLab.java, so that it throws the DateOutOfBoundsException. You should check for the month and year in the constructor: public DateExcepLab(int newMonth, int newDay, int newYear) throws DateOutOfBoundsException { if((newMonth <=0) || (newMonth > 12)) throw new DateOutOfBoundsException("Month must be 1<= m <=12"); else month = newMonth; ... if (newYear < MINYEAR) throw new DateOutOfBoundsException("Year "+newYear+" is too early"); else year = newYear; } Update UseDateExcep so that it also throws (re-throws) the DateOutOfBoundsException. Run the code and input dates which are out of bounds (month and then year), and see what is output.  (You just need to add throws DateOutOfBoundsException on the public static void main line.) Finally, modify UseDateExcep so that it catches and handles the exception itself, rather than throwing it. Run the code and input dates which are out of bounds (month and then year), and see what is output.  (Now you can remove the throws DateOutOfBoundsException on the public static void main line.).  Note that UseDateException has a method called getDate. public class UseDateExcep [ public static void main(String[] args) { DateExcepLab theDate; boolean DateOK = false; while (!DateOK) { System.out.println("Enter a date after the year " + DateExcepLabKEY.MINYEAR); System.out.println("For example, January 12, 1954, would be: 1 12 1954"); if (!getDate()) { System.out.println("non-numeric date entered"); return; } try { theDate = new DateExcepLab(month, day, year); DateOK = true; System.out.println("Your entered date is: "+theDate.toString()); } catch (DateOutOfBoundsException OB) { System.out.println(OB.getMessage()); } } } } KEY: Note that the KEY files posted in the syllabus also address non-numeric dates and invalid days   Recap: When dealing with error situations within our ADT method: Detect and handle the error within the method itself. Throw an exception related to the error. Ignore the error situation. Define the needed exception classes.