Java Learning Note (javase Bi Xiangdong version Video) three. 3

Source: Internet
Author: User

9, exception handling: The exception handling object in Java is to abstract the judgment of the previous if statement, and to form a set of error handling system. The top is throwable, followed by Error,exception, where Exception is clearly divided into two categories: RuntimeException and others.

1  Public classExceptions {2      Public Static intfunctionint[] arr,intIndexthrowsFushuindexexception//custom error class declaration thrown3     {4         /*Note that Java comes with an error-handling class that does not require the throws keyword to be declared and thrown to use,5 * Because these classes inherit from RuntimeException, the exception and its subclasses are not instrumented at compile time6 * But the runtime will error, and directly interrupt the program to run7          */8         if(arr==NULL){9             Throw NewNullPointerException ("Incoming array is a NULL pointer!") ");Ten         } One         if(index>arr.length-1){ A             Throw NewArrayIndexOutOfBoundsException ("Dude, the array angle is out of bounds!") "); -         } -         if(index<0){ the             Throw NewFushuindexexception ("Cannot use negative angle mark"); -         } -         returnArr[index]; -     } +      Public Static voidMain (string[] args)throwsFushuindexexception//thrown at the call -     { +         int[] arr={1,2,3,4}; A         /*catch processing is equivalent to resolving exceptions, and subsequent operations can continue to execute at * And if a piece of code can detect multiple exceptions, multiple catch statements are required, but only the first error is accepted - * Catch Statement if an object of type exception is accepted, due to polymorphism (subclasses can override parent class use) - * At this point, the statement can accept any type of exception, all if the sentence should be placed in the last -          */ -         Try{ -             intA=function (NULL, 2); in System.out.println (a); -}Catch(fushuindexexception F) { toSYSTEM.OUT.PRINTLN ("Negative angle mark is abnormal!") "); +SYSTEM.OUT.PRINTLN ("message:" +f.getmessage ()); -             //SOP Converts the content to a string in the output, and the object is no exception, so the ToString () operation is performed automatically theSystem.out.println ("string:" +f.tostring ()); *             /*the Getstacktrace () method can get the details of the error $ * Printstacktrace () is to print error messages to the console, which is the default method used by the JVMPanax Notoginseng              */ - f.printstacktrace (); the}Catch(NullPointerException N) { +SYSTEM.OUT.PRINTLN ("Incoming array is a NULL pointer!") "); A}Catch(Exception E) { theSystem.out.println ("Other errors"); +         } -         //finally statements are always executed, all of which are typically used to close/release resources $         finally{ $System.out.println ("Over"); -         } -System.out.println ("End"); the     } - }Wuyi /*The top-most error in Java is the Throwable class, where all available methods are defined and subclasses can be used directly.  the * Custom error classes must inherit the exception system (usually exception/runtimeexcetion) so that it can be thrown, - * And according to the specification should be the parent class as the end of the class name, improve the reading Wu * Custom Error classes If you want to be processed by Java, you must use two actions: - * 1, the declaration throws: after the need to throw the wrong function parameters with the throws keyword + custom error class name to throw, notice can be thrown more About  * () $ *!! Note that the throw is the same as in the Java processing mechanism, and it needs to be thrown at the call.  - * 2, using Try{}catch (Exception type variable) {}finally{} structure processing - * The above two ways if you can handle it with 2, can not handle the throw - *!! Note: Error-handling classes generally do not have to do too much, and the super-tired has been encapsulated, so you can directly call on the line, A *!! Creating custom error-handling classes is convenient for further classification of errors +  */ the classFushuindexexceptionextendsexception{ - fushuindexexception () { $          the     } the fushuindexexception (String msg) { the         Super(msg); the     } -}
Exception

1.

2.

3.

4.

10. Introduction to Object objects: This object is the parent of all objects and contains methods that are abstracted by all objects, so that each object of the method can use the

1  Public classObjectsuper {2      Public Static voidMain (string[] args) {3Objectsuperdemo o1=NewObjectsuperdemo ();4Objectsuperdemo o2=NewObjectsuperdemo ();5System.out.println (o1==O2);6         //in fact, the Equals method is to encapsulate the O1==O2 operation7 System.out.println (O1.equals (O2));8         /*Class object: All objects in Java (same as class, because containing the same content is extracted as a class object)9 * Class/interface generates a class file (i.e. bytecode file) after loading, and also generates a corresponding bytecode file object (class object) in memoryTen * Note: A class will only have a single byte-code file object in memory, and new objects are generated based on this object.  One * Use the object's GetClass () to get this object, also called the run-time class object A          */ -         /*toString (): The following argument for println () is a string type, so all types will be converted - * Where the base type is directly converted, and the object type is implemented by calling ToString (). As with Equals (), the * The Object object implements this method, but subclasses can overwrite it -          */ - System.out.println (O1.getclass ()); -     } + } - classobjectsuperdemo{ +      A } at classobjectsuperdemo1{ -ObjectSuperDemo1 (intnum) { - setnum (num); -     } -     Private intnum; -      Public intGetnum () { in         returnnum; -     } to      Public voidSetnum (intnum) { +          This. num =num; -     } the     /*the operation of Equals () overriding object is a common operation because it is judged by address * * In practice, it is often necessary to judge whether the object's specific content is the same, but instead of rebuilding the new method, you rewrite equals () $ * Robust judgment is required due to polymorphism and type conversion problemsPanax Notoginseng * will always be rewritten, and the Equals method in object is the same because it is the parent of all objects, if not implemented (abstract method) - * After each class, whether or not there are other functions, there is the first implementation of the function, inconvenient the      */ +      Public Booleanequals (Object obj) { A         if(! (objinstanceofObjectSuperDemo1)) { the             //return false; +             Throw NewClassCastException ("Type Mismatch"); -         } $ObjectSuperDemo1 o=(ObjectSuperDemo1) obj; $         return  This. num==O.num; -     } -      Public inthashcode () { the         returnnum; -     }Wuyi}
Object Objects

11 Package: When you use a compiler (such as Eclipse, etc.), when a class is established, you are prompted to select the package to manage the class, which is represented in the Windows system as a folder. If you do not manually create a Java file using the compiler, you should create the appropriate folder when using the package (you can use the command in Javac.exe/manual Setup) and move the class file into the folder to run.

Note points after using the package:

1, notice the class name should be added before the name of the package,

2, note that the path is the path of the package,

3. To be accessed by a class outside the package, the permissions of the class must be set to public, and the method must be public to be accessible by another package

One problem with packages is that you need to add a package name when writing a class, which is inconvenient. In Java, you can import the package. class file by using the Import keyword, and you can access it directly using the class name.

Jar Package: A jar file that compresses a package file in Java, and the jar file can be used to access the package without extracting it.

Java Learning Note (javase Bi Xiangdong version Video) three. 3

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.