Black Horse Programmer---exception summary

Source: Internet
Author: User
Tags throw exception

------- Android Training,Java Training,look forward to communicating with you! ----------
1: Exception
(1) Abnormal situation of the program.
(2) Exception inheritance architecture:
Throwable
|--error Serious problem, generally we can not solve.
|--exception
|--runtimeexceptionRun-time exception, this problem is generally to fix the code.
|--Non-runtimeexceptionCompile time exception, must be processed, otherwise the code can not pass.

(3) Default processing scheme for exceptions by the JVM:
By default, the class name of the exception, the cause, and the location where the error occurs are output in the console.

And from this place to terminate the program to run.

public class Exceptiondemo {public static void main (string[] args) {int a = 10;int b = 2;b = 0;//arithmeticexceptionsyste M.out.println (A/b); System.out.println ("Over");}}

(4) How we handle the program ourselves:
A:try...catch...finally
Basic format:
try {
There may be a problem with the code.
}catch (Exception class name variable name) {
Handling of exceptions.
}finally {
Frees resources. (IO, database operation is more common)
}
Morph Format:
                Try...catch...try...catch...catch...try...catch...finally...try...catch...catch...finally...try...finally ...



For multiple catch cases with one try:

The parent exception must be put last.
Try{}catch () {}catch () {}

New features of JDK7: This requirement is a peer relationship.

public class ExceptionDemo3 {public static void main (string[] args) {int a = 10;int B = 0;int[] arr = {1, 2, 3};try {Sys Tem.out.println (A/b); System.out.println (arr[3]);//... There may be a lot of code, but I'm not sure what the problem is. Swelling is done//solved with exception} catch (ArithmeticException e) {System.out.println ("divisor cannot be 0");} catch ( ArrayIndexOutOfBoundsException e) {System.out.println ("array index out of Bounds"),} catch (Exception e) {System.out.println ("Problem with program") ;} JDK7 Improved code try {System.out.println (A/b); System.out.println (Arr[3]);} catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {System.out.println ("minor problem with the program");} System.out.println ("Over");}}

B:throws
followed by the exception class name.
Location: After the () of the method.

Attention:
Don't throws if you can try...catch.
However, at present lectures for convenience, I all throws.
(5) What is the difference between a compile-time exception and a run-time anomaly?
A: The compile-time exception must be processed.
B: Runtime exceptions can be handled or not handled.
(6) The difference between throws and throw?
A:throws
Location: After the method (), followed by the class name.
If the following is based on RuntimeException and its subclasses, then the method can be used without processing.

If the following is based on exception and its subclasses, then the code must be written for processing, or thrown when called.

public class Teacher2 {public void checkscore (int score) throws Scoreexception {if (Score < 0 | | score >) {throw New Scoreexception ("score must be between 0-100");} else {System.out.println ("score OK");}}}

B:throw
Location: The name of the object followed in the method.
If the method has a throw throw runtimeexception and its subclasses, then there can be no throws on the declaration.

If there is a throw throw exception and its subclasses in the method, then the declaration must have throws.

public class Teacher {public void checkscore (int score)  {if (Score < 0 | | score >) {throw new scoreexception ("score must be between 0-100");} else {System.out.println ("score OK");}}

(7) Custom exceptions
You only need to inherit from exception or RuntimeException.
You can provide a construction method.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Black Horse Programmer---exception summary

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.