3.3 Java Foundation Summary ① exception ② Custom exception

Source: Internet
Author: User
Tags finally block getmessage modifiers throw exception try catch

① Exception ② Custom exception


I. Overview of issues in Java
In general there is a 2/8 principle, a programmer 80% of the energy to deal with 20% possible exception code
Problems in Java are divided into errors (error) and exceptions (Exception)

Error: An issue that cannot be solved with code, often a problem with the environment or hardware
Exception: An incorrect condition that can be resolved by modifying code or preprocessing, called an exception

Exceptions are divided into compile-time exceptions and runtime exceptions, and if a compiler exception is thrown, the caller must either handle it or throw it up, throw a run-time exception, and the caller will not throw

3 ways to end the inside of Java:
① Normal execution complete
②return return
③ exception occurred, no processing, forced end
is called at the return method.

How to handle Exceptions:
① catches an exception and does not let it continue to throw along the call stack
② captures, and continues to throw down
③ does not capture, causing the method to pop up from the call stack


Second, catch the exception try Catch finally

try{
Try block (attempt)
}catch (Exception Type) {
Catch block (SNAP)
}finally{
Finally block (always executed)
}

The try block, if an exception occurs, throws an exception, jumps to the corresponding catch block, jumps out after the catch block is executed, and (no more catch is executed). Finally, add exception to prevent an uncaught exception.
Exception to be written at the end, the catch has a sequence, there is an inheritance relationship, the subclass is in front, the parent class is behind.
Multiple catch can be picked up behind a try, and multiple exception handling in complex code can improve the user experience.

Finally block is always executed, even if there is Break,return, will execute finally and return, code level can close finally only system.exit ();

So here is generally used to clean up some resources, which are more closed than the stream, and the links are closed

Catch and finally must be matched to try, cannot appear alone, can have
Try Catch,catch
Try finally
Try Catch finally


Third, throw exception throw

Throw Exception method Sound place, followed by thrown exception type (statement)
Throws can be followed by a variety of exception types, subclasses throw exceptions can not be more than the parent class (not the number, but the scope)
Method declares the full format: access modifiers, optional modifiers, return type, method name (parameter list) throws exception type

Throwbale here are two ways to be more important:
-getmessage (Get exception information, not all exceptions have exception information, here information such as the divisor is 0)
-printstacktrace (print stack information)

Throw throws exception (active throw) is very useful,
Throw a compiler exception, the method must be processed (Trycatch or thrown up), if thrown up, the caller must be trycatch or thrown up
Throws a running exception, can not be thrown up, or trycatch

Iv. Custom Exceptions:
① Create a class inheritance exception
② adding construction methods and common methods
The construction method is mainly a variety of exception types, the common method can write logs and so on
③ throws an exception in a method
④ catches the exception in another method and handles

Benefits: Do not take care of each place, go straight up
Throw all the exceptions, throw all the anomalies into the presentation layer, handle them in the presentation layer, and call the write log of the custom exception to save it.

public class Lovoexception extends exception{

Public Lovoexception () {
}

Public lovoexception (Exception e) {
JVM-generated exceptions, using multiple
Super (E);
}

Public lovoexception (String message) {
Based on business exceptions, constructs, uses less
Super (message);
}

public void Writelog () {
Various log files
This.getstacktrace ();
System.out.println (This.getmessage ());
}
}

public void count () throws Exception {

int a = new Scanner (system.in). Nextint ();
try {
int b = 3/a;
System.out.println (3/a);
} catch (Exception e) {
throw new Lovoexception (E.getmessage () + "divisor cannot be 0");
throw new Lovoexception (e);
}
}

3.3 Java Foundation Summary ① exception ② Custom exception

Related Article

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.