Exceptions in Java

Source: Internet
Author: User

Concept of 1.java anomaly

  Exceptions are errors that occur during the program's run, and the exception class inheritance relationships in Java are as follows:

Error: An internal system error that occurs during the program's operation, which can only notify the user of such an error and cannot be processed for this type of error

runtimeexception: Access null pointers, array out-of-bounds access, etc. This type of error is definitely your problem, and the problem arises because you are not checking the program to avoid exceptions.

Other exception: An attempt was made to open a nonexistent file and attempt to read data at the end of the file.

Exceptions in Java can be categorized into the following two types:

(1) No exception checked: Runtimeexception,error

(2) Checked exception: Other exception

The exception handling mechanism in Java is primarily a checked exception that occurs during the processing of a program.

2. Exception base class

  Throwable

{

Throwable () {}

Throwable (String message) {}

String getMessage () {}

}

  Throwable is the base class for exceptions, and the Throwable base class has two constructors, the default parameterless constructor, and a constructor with a string argument. However, when customizing their own exception classes, it is common to inherit the exception class, which is usually added with these two constructors. There is also a getmessage () method inside the Throwable class, which can obtain information about the exception.

  

Use of exceptions in 3.java

  (1) Declaration of exception

  void Fun () throws Exception

{

}

This syntax means that declaring this method throws an exception, in which case the exception and its subclass exceptions can be handled or thrown, and a method should try to declare the checked exception that appears in the method.

  (2) Exception throws

  void Fun () throws Exception

{

throw new Exception ();

}

An instance of an exception to throw when an exception is thrown

  (3) Abnormal capture

void Fun () throws Exception

{

try{

throw new Exception ();

}

catch (IOException e) {

}

catch (Exception e) {

}

finally{

}

}

Using a try Catch statement to catch an exception, the two keywords must appear in pairs, throw an exception in the try, and catch to handle the caught exception. After the try throws an exception, the statement following the throw exception is no longer executed, and the statement in the catch is executed by going to the catch block.

If a resource is used in the program, the program has an error during the run. When you need to release resources, you need to use the finally statement. After the program has an error, the program goes into the catch statement and then executes in the finally statement, freeing the resource in finally. Regardless of whether an error occurs in the program, the statement in the finally is executed after the statement in the try and catch is finished.

A try can have multiple catch statements, and the error information generated in the Try statement matches the error type in the catch, matches the best error, and then enters into the catch statement block to execute the related statement.

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.