packagecn.china;/*** Exception: Abnormal conditions that occur during Runtime. * A lot of problems, meaning the description of the class is also a lot, * Its generality is extracted upward, forming an anomaly System. * The final question is divided into two main categories: * throwable: whether it is an error, or an exception, is a problem, it should be thrown, let the caller know and Handle. * This system is characterized by the fact that Throwable and all of its subclasses are Parabolic. * How to embody the parabolic nature? * In fact, through two key words embodied, throws,throw; The classes and objects that can be manipulated by these two keywords have the ability to be parabolic. * 1, General non-processing, Error * Features: is the severity of the problem thrown by the jvm, has affected the implementation of our program; This problem occurs generally not targeted processing, direct modification of the program * 2, can be processed, Exception * features: subclass suffix is used by its parent class Name as a suffix, reading is very strong. * * Exception classification: * 1, Compile-time is detected exception: as long as the exception and its subclasses are, in addition to RuntimeException and its subclasses * This problem once appeared, want to test at compile time, let this problem have corresponding processing way. * This problem can be targeted to deal with * 2, Compile-time does not detect exceptions (runtime exceptions): runtimeexception and its subclasses. * This problem occurs, cannot allow the function to continue, the operation can not be carried out, more because of the cause of the call or caused the internal state changes Caused. * This problem is generally not handled, directly compiled through, at run time, let the caller call the program to force stop, let the caller adjust the Code. * * Throw and throws difference? * Throws uses the exception class thrown on the function, can throw multiple can be separated by commas * Throw using the exception object thrown inside the function*/classFushuindexexceptionextendsException//so you can inherit runtimeexception, you don't need to declare or catch exceptions, and the compilation won't Fail.{ publicfushuindexexception (String Meg) {Super(meg); //TODO auto-generated Constructor stub }}classd{ public intMethodintarr[],intIndexthrowsfushuindexexception {if(arr==NULL) Throw NewNullPointerException ("the reference to the array cannot be Empty!") "); if(index>=Arr.length)Throw NewArrayIndexOutOfBoundsException ("the Array is out of bounds!" "+index); if(index<0)//is intended to be represented by a Negative-angle-mark exception, but this exception is not defined in Java. According to the idea of Java exception creation, the negative angle is defined as a custom description and encapsulated into an object Throw NewFushuindexexception ("the angle of an array cannot be a negative number" +index); returnarr[index]; }} public classDemo { public Static voidMain (string[] Args)throwsfushuindexexception {int[]arr=New int[3]; D d=NewD (); intnum= D.method (arr, 30); } }
packagecn.china;/**catch form of exception handling * Specific Format: * Try * {code to be detected exception} * Catch (exception class variable)//the variable is used to receive the object that the exception occurred * {the code that handles the exception} * finally * {the code that will be executed} * * Exception Handling Principle: * 1, inside the function if you throw an exception that needs to be detected, then the function must be declared, otherwise it must be caught with trycatch within the function, or the compilation will Fail. * 2, If the call to declare the exception function, either Trycatch or throws, or the compilation Fails. * 3, when catch, when throws? * Function can be resolved internally, with catch; * Cannot solve with throws, tell the caller, the caller solves; * 4, a function if you throw multiple exceptions, the call must be handled by the corresponding multiple Catch. * Inside there are several anomalies to be detected, just throw a few exceptions, throw a few, on the catch several;*/classFushuindexexceptionextendsException//so you can inherit runtimeexception, you don't need to declare or catch exceptions, and the compilation won't Fail.{ publicfushuindexexception (String Meg) {Super(meg); }}classc{ public intMethodintarr[],intIndexthrowsfushuindexexception {if(arr==NULL) Throw NewNullPointerException ("the reference to the array cannot be Empty!") "); if(index>=Arr.length)Throw NewArrayIndexOutOfBoundsException ("the Array is out of bounds!" "+index); if(index<0)//is intended to be represented by a Negative-angle-mark exception, but this exception is not defined in Java. According to the idea of Java exception creation, the negative angle is defined as a custom description and encapsulated into an object Throw NewFushuindexexception ("the angle of an array cannot be a negative number" +index); returnarr[index]; }} public classDemo1 { public Static voidmain (string[] Args) {int[]arr=New int[3]; C C=NewC (); Try { intnum= C.method (arr, 30); System.out.println ("num" +num);//num is a local variable so these two sentences are placed in the same block of code } Catch(nullpointerexception E) {//Multiple Catch formatsE.printstacktrace (); } Catch(arrayindexoutofboundsexception E) {e.printstacktrace (); } Catch(fushuindexexception e) {e.printstacktrace ();//the default exception handling mechanism of the JVM is the method that invokes the exception Object. System.out.println ("Message" +E.getmessage ()); System.out.println ("String" +e.tostring ()); System.out.println ("negative angle marks are abnormal!" "); } Catch(Exception E) {//the parent class needs to be placed at the bottom, or the compilation fails because exception matches all exceptions,E.printstacktrace (); } System.out.println ("over!"); }}
packagecn.china;/**Try Catch finally combination features * 1, Try Catch finally * 2, try catch (multiple when no necessary resources need to be freed, can not define Finally) * 3, Try finally * Example: V OID Show () throws Exception//because there is no catch, you must declare the exception * {* try{*//open resource * throw new Exception *} * Finally{*//close resource * } * }*/classe{ public intMethodintIndex) { int[]arr=New int[3]; if(index>=Arr.length)Throw Newarrayindexoutofboundsexception (); returnarr[index]; }} public classDemo2 { public Static voidmain (string[] Args) {e e=NewE (); Try{intNum=e.method (3); System.out.println (num); }Catch(arrayindexoutofboundsexception c) {System.out.println (c.tostring ()); return; //system.exit (0); exit the JVM only this case does not execute the finally statement; } finally{//statements that are bound to be executed, primarily for shutting down or freeing resourcesSystem.out.println ("finally"); } System.out.println ("over!"); }}
packagecn.china;Importjava.time.format.TextStyle;/*** Mr. BI uses the computer class, * Analysis of the problem: * computer Blue screen, Smoke*/classLanpingexceptionextendsexception{lanpingexception (String Msg) {Super(msg); }}classMaoyanexceptionextendsexception{maoyanexception (String Msg) {Super(msg); }}classNoplanexceptionextendsexception{noplanexception (String Msg) {Super(msg); }}classcomputer{Private intState=0; public voidRun ()throwslanpingexception,maoyanexception {if(state==1) Throw NewLanpingexception ("blue Screen")); if(state==2) Throw NewMaoyanexception ("the Computer is smoking"); System.out.println ("computer run"); } public voidReset () { state=0; System.out.println ("the Computer restarted."); }}classteacher{PrivateString name; PrivateComputer comp; Teacher (String Name) { this. name=name; Comp=NewComputer (); } public voidPrelect ()throwsnoplanexception {Try{comp.run (); System.out.println (Lecture); }Catch(lanpingexception e) {System.out.println (e.tostring ()); Comp.reset (); Prelect (); }Catch(maoyanexception e) {System.out.println (e.tostring ()); Test (); Throw NewNoplanexception ("class cannot be done" +E.getmessage ()); //the computer can be repaired//Throw e; } } public voidTest () {System.out.println ("everybody practice."); }} public classDemo3 { public Static voidmain (string[] Args) {Teacher Teacher=NewTeacher ("mr. Bi")); Try{teacher.prelect (); } Catch(noplanexception e) {System.out.println (e.tostring ()+"......"); System.out.println (Substitution); } }}/*** Class Noaddexception extends exception{} * void AddData (Data d) throws noaddexception * {* linked database * try{* Add database, exception SQLException (); *}catch (SQLException E) * {*//processing code * throw new noaddexception (); *}finally * {* * * Close Database *} *}*/
packagecn.china;/**Exception Note * 1, subclasses when overriding the parent class method if the parent class method throws an exception, then the method of the subclass can only solve the exception of the parent class, or the subclass of the exception * 2, if the parent class throws more than one exception, then the subclass can also throw a subset of the parent class exception * * Simple: subclass overrides the parent class, Only exceptions, or subsets, of the parent class can be thrown, and if the parent class does not throw an exception, the subclass must never throw an exception when overridden, but can be used in try;*/classAextendsException {}classBextendsa{}classCextendsexception{}classfu{voidShow ()throwsa{}}classZiextendsfu{voidShow ()throwsb{}//can only throw a or B}classtest{voidmethod (Fu F) {Try{f.show (); }Catch(a a) {} }} public classDemo4 { public Static voidmain (string[] Args) {Test t=NewTest (); T.method (NewZi ()); }}
Java===exception Exception Handling