Summary of Java Foundation exceptions

Source: Internet
Author: User

Exception: What is an exception? Is the description of the problem, the problem is encapsulated----------exception system: Throwable |--error |--exception |--runtimeexception anomaly System features: All classes in the anomaly system are And the objects created are parabolic, which means they can be manipulated by the throw and throws keywords. Only the anomaly system has this feature. ---------------throw and throws usage: Throw is defined within a function and is used to throw an exception object. Throws is defined on the function, used to throw the exception class, you can throw more than a comma separated when the function is thrown with a throw exception object, do not do a try processing, must be declared on the function, both compile failed. Note that, except for runtimeexception, that is, if the runtimeexception exception is thrown inside the function, the function can be declared. If the function declares an exception, the caller needs to handle it, and the processing method can throw to the try. There are two kinds of exceptions: compile-time is detected exception at compile time, if there is no processing (no throw and no try), the compilation fails. The exception is identified, which means that this can be handled. Runtime exceptions (not detected at compile time) at compile time, no processing is required and the compiler does not check. This exception occurs, it is recommended not to process, let the program stop, the code needs to be corrected.   Exception Handling type: 1.  Try{}catch () {}2.  Try{}catch () {}finally{}3. try{}finally{} Note: It is usually defined in 1,finally that the resource code is closed because the resource must be freed 2,finally only one case will not be executed when executed to System.exit (0); Finally will not be executed. Custom Exceptions: Define class inheritance Exception or RuntimeException1, in order for the custom class to be parabolic. 2, let the class have the common method of operation exception when you want to define the information for a custom exception, you can use the functionality already defined by the parent class. Class MyException extends Exception{myexception (String message) {super (message);}} Custom exceptions: Encapsulate specific issues that arise in your program in the Java object-oriented mindset.-----------------------------------------------------------------------exceptionBenefit: 1, encapsulate the problem as an object. 2, the normal process code and problem-handling code are separated to facilitate reading exception processing principle: 1, there are two ways of processing: try or throws. 2, called to throw an exception when the function throws several, on the handle several. A try corresponds to multiple catch. 3, multiple catch, the parent's catch is placed at the bottom. 4,catch, you need to define the specific processing, do not simply define the printstacktrace, or the output statement can not write the content when the catch to the exception, this function can not be processed, you may continue to throw in the catch Try{throw new aexception ();} catch (Aexception e) {throw e;} If the exception is not handled, it does not belong to the exception that is present with the feature. Exceptions can be thrown after the exception is cast and associated with that function. Or an exception can be handled, but it is necessary to provide the problem related to the function of the exception provided when the caller knows, and processing, can also be caught after the exception is processed, the new exception is converted. Try{throw new Aexception ();} catch (Aexception e) {//to aexception handle throw new Bexception ();} Example: Remittance Example Exception Note: When the child parent class overrides: 1, the subclass throws an exception subclass, or subset 2, if the parent class or interface does not throw an exception, the subclass overrides an exception, only try cannot throw class {public static void main ( String[] args) {System.out.println ("Hello world!");}}
/* There is a circle and a rectangle can get the area, for area if the illegal value, considered to be the problem of obtaining an area of an issue through the exception to indicate the existing basic design of the program. */class Novalueexception extends runtimeexception//has this class to make program calls more intuitive {novalueexception (String message) {super (message) ;}} Interface Shape{void Getarea ();//own characteristics, extraction does not come out, so no parameters}class Rec implements shape{private int len,wid; REC (int len,int wid)//throws novalueexception Here the comment cancels out try,try do not know the problem (here the exception throws the declaration, need to work within the function, such as cancel, try also can not handle) {if (len<=0 || wid<=0) throw new Novalueexception ("illegal value");/*system.out.println ("wrong"); After the judgment is added, Getarea () will continue to run and learn the anomaly, and it sucks. The next step is to replace it with an unusual method. ^_^ */this.len = Len;this.wid = wid;}  public void Getarea () {System.out.println (LEN*WID); }}class Circle implements Shape{private int radius;public static final double PI = 3.14;    Circle (int radius) {if (radius<=0)//throw new RuntimeException ("Fly to Fly"); throw new Novalueexception ("Fly to Fly"); This.radius = radius;} public void Getarea () {System.out.println (RADIUS*RADIUS*PI);}} Class Exceptiontest2{public static void Main (string[] args) {rec R = new Rec (3,4); R.getarea (); CirCLE C = New Circle (-8); System.out.println ("over");/*catch (Novalueexception e) {System.out.println (e.tostring ());} System.out.println ("over"); */}}

Summary of Java Foundation exceptions

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.