* Exception Precautions:
1, when a subclass overrides a parent class method, the method of the parent class throws an exception, and the child class's method can only throw the exception of the parent class or the subclass of the exception.
2, if the parent throws more than one exception, the child class can only throw a subset of the parent class exception.
Simply put: Subclasses overriding the parent class can only throw exceptions or subclasses or subsets of the parent class.
Note: If the method of the parent class does not throw an exception, then the subclass must not be thrown when overridden, only try.
* Principle of exception handling:
1, function content if you throw an exception that needs to be detected, then the function must be declared.
Otherwise, it must be captured within the function with Trycatch, otherwise the compilation fails.
2, if the function that declares the exception is called, either Trycatch or throws, or the compilation fails.
3, when catch, when throws?
The function content can be solved, with catch.
Can not solve, with throws to tell the caller, by the caller resolved.
4, if a function throws more than one exception, then the call must have a corresponding multiple catch for targeted processing.
There are several exceptions that need to be detected inside, throw a few exceptions, throw a few, catch several.
* Classification of exceptions:
1, compile-time is detected exception: As long as the exception and its subclasses are, in addition to the special sub-class RuntimeException system.
Once this problem occurs, it is hoped that it will be detected at compile time, so that the problem can be handled in a corresponding way.
Such problems can be dealt with in a targeted way.
2, no exception is detected at compile time (runtime exception): Is the runtimeexception in exception and its subclasses.
This problem does not allow the function to continue, the operation can not be carried out, more because of the caller's cause or caused by the internal state changes caused.
Then this problem is generally not handled, directly compiled through, at run time, let the caller call the program to force stop, let the caller to modify the code. So when customizing exceptions, either inherit exception. or inherit runtimeexception.
The difference between *throws and throw
1,throws used on a function
Throw is used within a function
2,throws throws an exception class that can be thrown multiple, separated by commas.
Throw throws an exception object.
1 classBlueexceptionextendsexception{2 Publicblueexception (String message) {3 System.out.println (message);4 }5 }6 7 classBreakexceptionextendsexception{8 Publicbreakexception (String message) {9 System.out.println (message);Ten } One } A - classHanderexceptionextendsexception{ - Publichanderexception (String message) { the System.out.println (message); - } - } - + classcomputer{ - Private intstatus; + Public voidWorkintStatus,string name)throwsbreakexception, blueexception{ A if(status==1){ at Throw NewBlueexception ("Computer blue screen, please restart"); -}Else if(status==2){ - Throw NewBreakexception ("Computer thorough, please call Customer service ..."); -}Else{ -System.out.println ("Normal class, teacher name:" +name); - } in } - Public voidReset () { toSYSTEM.OUT.PRINTLN ("Computer Reset success ..."); + } - } the /** * * Compile time exception must be handled, run time exception can be handled also can not handle $ * @authorSunfeiPanax Notoginseng * - */ the classteacher{ + PrivateString username; A Privatecomputer computer; the PublicTeacher (String useranme,computer computer) { + This. username=Useranme; - This. computer=computer; $ } $ Public voidTeacherintStatusthrowshanderexception{ - Try { - This. Computer.work (Status, This. username); the This. Computer.reset (); -}Catch(breakexception e) {Wuyi System.out.println (e.tostring ()); the This. replace (); - Throw NewHanderexception ("Customer service is coming ..."); Wu}Catch(blueexception e) { - System.out.println (e.tostring ()); About go (); $ } - } - - PublicString GetUserName () { A returnusername; + } the Public voidSetusername (String username) { - This. Username =username; $ } the PublicComputer Getcomputer () { the returncomputer; the } the Public voidsetcomputer (computer computer) { - This. Computer =computer; in } the Public voidGo () { theSystem.out.println ("Continue after reboot ..."); About } the Public voidreplace () { theSystem.out.println ("Continue after substitution ..."); the } + } - the Public classExample2 {Bayi Public Static voidMain (string[] args)throwshanderexception { theComputer computer =Newcomputer (); theTeacher T =NewTeacher ("Zhang San", computer); -Scanner Scanner =NewScanner (system.in); -System.out.println ("Please enter status"); the intStatus =scanner.nextint (); the System.out.println (T.getusername ()); the while(status!=0){ the t.teacher (status); -status=scanner.nextint (); the } the } the}
Exception handling of Java learning