Java Learning Diary Num7

Source: Internet
Author: User

Learning Content : abnormal system; A classic example of exception handling; package

  first, abnormal system 1, Anomaly conceptexception is a description of the problem, according to the Java object-oriented thinking of the problem of object encapsulation  2, abnormal systemthe abnormal eldest brother is Throwable, below has two younger brother respectively is error and exception. Error is a serious mistake, and it is generally not written in a targeted code to handle it. The other is exception, non-serious errors, generally to write targeted processing code. In exception's little brother, there is a small brother Runtimeexceptin (run-time exception), the exception in this small anomaly system, can not throw, do not declare, do not handle, the program is compiled through, so normal execution, very special bar, haha.   3, the characteristics of abnormal systemall the classes in the anomaly system and the established objects are parabolic, which means they can be manipulated by the throw and throws keywords, and only the exception system has this feature.   usage and differences of 4,throw and throws keywordsThe throw is defined inside the function and is used to throw an exception object. throws is defined on a function, used to throw an exception class, can be thrown multiple, separated by commas. when there is a throw exception object inside the function, it is not processed, it must be declared on the function, otherwise the compilation fails, but runtimeexception this special exception, that is, the runtimeexception exception is thrown inside the function, function can not be declared. If an exception is declared on a function, the caller must be handled in the appropriate way, either by throwing it to the previous caller, or by handling itself internally.   5, exception handling statementTry{ The code that needs to be detected by the exception; }catch (){code to handle exceptions}finally{code that is bound to execute}  6, several combination formats of exception statementstry               try           try {}                 {}          { } catch              finally      catch () {  }               {   }       { }                                     finally                                  {  }   7, custom exceptiondefine exception class inheritance exception or RuntimeException1), in order for the class to have a parabolic2), so that the class has a common method of operating anomalies  8, the principle of exception handling1), there are two ways of handling exceptions, one is to handle the try or throw out throws let the caller handle2), called to throw the exception of the function, throw a few, just deal with a few, a try can correspond to multiple catch3), multiple catch, the parent's catch is placed at the bottom4), within the catch, you need to define a specific processing method, do not simply define the output statement, and do not handle the problem and hide.   9, a very important idea of abnormal handlingwhen an exception is caught, this feature cannot be processed and can continue to be thrown in the catch. if the caught exception does not handle the exception that is not part of the feature, you can convert the exception and then throw the exception associated with that feature. Or an exception can be handled, and when it is necessary to provide the problem related to this function, let the caller know and handle, can also be caught in the exception after processing, to transform the new exception.   10, when the child parent class overrides, the handling principle of the exception1), the exception that is thrown by the subclass must be a subclass or a subset of the exception of the parent class. 2), if the parent class or interface does not throw exception, self-covering exception, can only handle themselves, that is, try processing, absolutely cannot throw  11, a matter of notethe statement below the throw and return will not be executed, and the compilation will fail in this case.   Second Classic exception handling examples Requirements: to the teacher to use the computer, and the computer may appear a variety of faults, such as blue screen, or hardware damage and so on, blue, the teacher can handle, restart the computer on the line, if the hardware is damaged, the teacher can not handle, But this exception has nothing to do with the way the teacher calls the lecture, but he can convert it to his own exception and throw it to the top. class Bluescreenexception extends Exception{bluescreenexception (String msg){super (msg);}}class Smokingexception extends Exception{smokingexception (String msg){super (msg);}}class Noplanexception extends Exception{noplanexception (String msg){super (msg);}}class Computer{private int state=3;Public Void Run () throws Bluescreenexception,smokingexception{if (state==2){throw new Bluescreenexception ("blue Screen");}if (state==3){throw new Smokingexception ("Computer is smoking");}System.out.println ("computer is running");}Public void Reset (){state=1;System.out.println ("The computer has been restarted");}}class Teacher{Public Void Teach () throws Noplanexception{Computer cmpt=new computer ();Try{Cmpt.run ();System.out.println ("Lectures");}catch (bluescreenexception e){Cmpt.reset ();System.out.println ("Lectures");//cmpt.run ();}catch (smokingexception e){     Test (); throw new Noplanexception ("The class cannot continue because the computer is broken");}}Public Void Test (){System.out.println ("practice");}}class TestException{Public static void Main (string[] args){Teacher t=new Teacher ();Try{T.teach ();}catch (noplanexception e){System.out.println (e.tostring ());System.out.println ("The Holiday Live Contact");}}}  three, the package  1. Overview of the package1) Classification management of class files2) provide multiple layers of namespaces to classes3) write in the first line of the program file4) The name of the class becomes the package name. class Name5) package is also a form of encapsulation.   2, the appearance of the package access features1) Access between the package and the package, the classes in the package being accessed, and the members in the class require public adornments2) classes in different packages can directly access members that are decorated with protected permissions in the parent class3) There are only two types of permissions that can be used between packages and packages: public and protected  3,import KeywordsWhen a package appears, access to the class between the package and the package requires the full name of the class, which is the package name. The Import keyword is introduced to simplify writing. The import importer is the class in the package, and there are no child packages in the package. When importing imports, do not use the * wildcard character, you need to use the class in the package, import that class, because the use of wildcards, too memory-intensive. If you have two classes that have duplicate names, add the name of the class name before the class is used.   4,jar Bagjar package is a kind of Java compression packageeasy to carry itemseasy to use, just set the jar path under Classpaththe database-driven SSH framework is a jar package

Java Learning Diary Num7

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.