Java language Programming 5 exceptions on the computer experiment

Source: Internet
Author: User
Tags stub

Java Language Programming on the computer experiment 5

Experimental Purpose :

Understanding the concept of anomalies, mastering Java 's exception handling mechanism, common exception capture methods, custom exceptions and their throwing and capturing methods.

Experiment content:

    1. Catch of common exceptions:

Write a program that generates each Exceptions for type arrayindexoutofboundsexception and numberformatexception types, and then rewrite the program, These two types of exceptions are captured and handled reasonably.

Also, think and answer the question : can 0 simultaneously throw arrayindexoutofboundsexception types and NumberFormatException type of exception? Why?

    1. Define, discard, and capture custom exceptions:

(1) custom Two exception class: illegal name exception Illeganameexception and illegal address exception illegaladdressexception.

(2)definitionStudentclass containsNameand theAddressProperties, andSetName,setaddressmethod, when the name length is less than1or greater than5thrown whenilleganameexceptionwhen the address does not contain a "province" or "City" keywordillegaladdressexception.

(3) The writer throws these two exceptions, captures them in the main method and handles them reasonably.

Experiment Requirements:

1. The results of the program operation are given in the experimental report .

2. The source code is attached to the end of the experimental report.

3. Carefully fill in the experimental report and properly archived, before the next Computer Experiment class to send an electronic version of the experimental report to the [email Protected].

Note thatthe message header and Attachment Lab report file name are:

JAVA1 Experimental report n_ _ name

which N is an Arabic numeral, refers to the first several experiments, for example :

Java 1 Experimental reports 3 _ 2012010400_ li Lei

please strictly in accordance with the prescribed format Send Mail (Note the "Java" number in the title 1 ") , the message header does not meet the formatting requirements equal to the non-delivery of the job, and at the specified time to send the experiment report mail, expired invalid.

4. The same person will not be able to get the corresponding normal points in the experiment report.

Appendix: Experimental Reports

Experimental Topics _ _ Experiment 5 ________

School Number __2014011329__ Name ___ Xu Qiai ____ Class ___ computer 14-1___ Time ____________

Experiment Topic Answer

A

Two

attached : Source program

A

 Packagecom. TEST5; Public classTEST5 { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        inta[]=New int[8]; intb; Try{             for(inti=0;i<10;i++) {System.out.println (a[i]); }        }        Catch(arrayindexoutofboundsexception e) {System.out.println ("Array Out of Bounds"); }        Try{b=integer.parseint ("23GF"); }        Catch(NumberFormatException e) {System.out.println ("Type format conversion error"); }}} two. Test55.java Packagecom. Test55; Public classTest55 {/**     * @paramargs *@throwsillegaladdressexception *@throwsilleganameexception*/     Public Static voidMain (string[] args)throwsillegaladdressexception, illeganameexception {//TODO auto-generated Method StubStudent s=NewStudent (); S.setname ("Test name string"); S.setaddress ("Test address string"); }}student.java Packagecom. Test55; Public classStudent {String Name;    String Address;  Public voidSetName (String Name)throwsilleganameexception{if(Name.length () <1| | Name.length () >5)            Throw NewIlleganameexception (name+ ", error in length, should be between 1-5.) ");  This. Name=Name; }     Public voidSetaddress (String Address)throwsillegaladdressexception{Booleanj=false;  for(intI=0;i<address.length (); i++){            if(Address.charat (i) = = ' Province ' | | Address.charat (i) = = ' City ') {J=true;  Break; }                        }        if(j==false){            Throw NewIllegaladdressexception (address+ "No ' province ' or ' city ' appears, please fix"); }         This. address=Address; }}illeganameexception.java Packagecom. Test55; Public classIlleganameexceptionextendsException { Publicilleganameexception () {Super(); }     Publicilleganameexception (String s) {Super(s); }}illegaladdressexception.java Packagecom. Test55; Public classIllegaladdressexceptionextendsException { Publicillegaladdressexception () {Super(); }     Publicillegaladdressexception (String s) {Super(s); }}

Experiences and experiences:

after programming the exception to understand his meaning and usage, follow the rules in Java, just provide a platform to establish an exception, the definition is also outside, before understanding the error, that the new exception is in a class, it is very helpful to me.

Java language Programming 5 exceptions on the computer experiment

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.