Java Notes--exceptions

Source: Internet
Author: User

1, the exception is divided into two categories:

--1) Error:java serious problems that the virtual machine cannot solve (such as resource exhaustion, etc.);

--2) Exception: General issues caused by other programming errors or accidental external factors (such as null pointer exceptions, nonexistent files being read, network outages, etc.).

2. The most desirable catch error is during compilation, and then some errors occur only at run time (compile-time exception (checked), run-time exception (unchecked)).

3. Common run-time exceptions:

--1) array subscript out of bounds: arrayindexoutofboundsexception;
--2) arithmetic anomaly: arithmeticexception;
--3) type conversion exception: ClassCastException;

--4) NULL pointer exception: NullPointerException;

4, Java provides a catch-and-throw model of exception handling.

--If an exception occurs during the execution of the program, an exception class object is generated, which will be presented to the Java operating system, which is called throwing an exception;

--The exception object can be generated automatically by the JVM, or it can be created manually by developers.

5, try{}

catch (Exception1 E1) {}//catch can write more, the exception class object thrown in the try matches the exception class type in the catch from the top down.

catch (Exception2 E2) {}//executes the code in the catch once it is satisfied, and the other catch after that catch is not matched.

finally{//is bound to be executed code}//finally is optional

6, for run-time exceptions, can not be displayed exception handling, but for compile-time exceptions, you must do exception handling.

7, if there are multiple catch statements, and the exception class object has an inheritance relationship, the subclass must be written in front of the parent class, otherwise it cannot be compiled.

8. If a finally statement exists, the statement inside the finally is executed before the exception is handled.

9. If you do not know how to handle the exception, you can display the type of the exception object at the method declaration, which is handled by the caller of the method (throws), and if the caller does not know how to handle the exception, continue to throw the class exception to the caller, up to the virtual machine for processing.

10. The Exception class object that is thrown manually can be a provided exception class, or it can be a custom exception class.

--Example:

 public  class  myexception extends   exception{ static  final  long  serialversionuid =-123435; //  provide a unique serial number  //     provide several constructors  public   MyException () { ();  public   MyException (String str) {super   (STR); }}

11. Subclasses override the parent class's method, which throws an exception type that can only be the subclass or exception type of the exception class of the overridden method.

12, the exception summary (catch-and-throw model):

--try: Executing code that could produce an exception

--catch: Catching exceptions

--finally: Code is always executed regardless of whether an exception occurs

--throw: Exception occurrence phase (manually throwing exception objects)

--throws: Exception handling (various exception classes that declare methods may be thrown)

Java Notes--exceptions

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.