Exceptions in Java

Source: Internet
Author: User

I. Concepts and CLASSIFICATIONS

In the Java programming language, exception objects are an instance of the Throwable class.

Where the error class hierarchy describes the internal and exhaustion errors of the Java Runtime System. This error cannot be manipulated and will not be thrown. When designing Java programs, we need to focus on exception.

Exception consists of two branches: RuntimeException and other exceptions. The rules for dividing two branches are: exceptions caused by program errors belong to RuntimeException, and the program itself is not a problem, but exceptions due to problems such as I/O errors are other exceptions.

RuntimeException is a run-time exception, that is, the exception that occurs when the program is running, also known as "unchecked exception", meaning "exception not checked by the compiler".

  

Exceptions that derive from RuntimeException contain the following scenarios:

    • The type conversion of the error.
    • Array access out of bounds
    • Accessing a null pointer

Other exceptions include:

    • Trying to read data behind a file question
    • An attempt was made to open a file that does not exist.
    • An attempt was made to look up a class object based on a given string, and the string represented by it does not exist.

Ii. Declaration of exception

In the Java method, if you may encounter an unhandled situation, such as the file to open does not exist, then declare the exception. The way to declare an exception is to declare all possible exceptions at the header of the method. For example, the declaration of a constructor for a FileInputStream class in a standard class library:

  

A method must declare all of the checked exceptions that may be thrown (exceptions other than runtimeexception in exception), without checking that the exception is either uncontrollable (Error) or should be avoided (runtimeexception).

If a method does not declare all of the checked exceptions that may occur, the compiler will make an error.

Third, the exception throws

Exception throws using the "throw" keyword, such as a method may appear eofexception, we want it to throw this exception, you can use the following statement:

  Throw New Eofexcetion ();

Or: 

New eofexception (); throw E;

If a method throws an exception using the Throw keyword, and the exception is not runtimeexception, then the exception must be declared with throws in the method header.

If a method declares an exception with throws, the method that invokes the method must either catch the exception or continue declaring it. (This is a little exception, if the parent method does not declare an exception, then the subclass cannot declare an exception when overriding the method of the parent class, see jcomponent.paintcomponent method)

Iv. Creating exception classes

Sometimes we may encounter problems that all standard exception classes cannot handle accurately, such as int age =-1; This line of code is not a problem in itself, but in reality it is not possible to be negative. At this point, we need to create an exception class ourselves:

  

  

V. Abnormal capture

If an exception is not captured anywhere, the program terminates execution and prints the exception information on the console, including the type of exception and the stack and contents.

To catch an exception, you must set the Try/catch statement block:

  Try  {somecode;  }catch for this Exception; }

    • if any code in the try statement block throws an exception class that is described in the catch clause, then The program skips the rest of the code in the TRY statement block and executes the processing code in the catch clause.
    • If the code in the TRY statement block does not throw any exceptions, the program skips the catch statement.
    • If any code in the method throws an exception type that is not declared in the catch clause, the method exits immediately.

  

Exceptions in Java

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.