The basics of implementing exception handling in Java

Source: Internet
Author: User

The basics of implementing exception handling in Java

Exception (Exception): Occurs during program execution, indicating that an illegal health condition has occurred. Many of the methods in the JDK throw an exception object when an illegal condition is detected.

For example: array is out of bounds and is removed by 0.

Source code Example:

Package Yanzheng;

About the exception 2015.11.11

Import javax.swing.*;

public  class  aboutexception {

public static void main (string[] args)

{

       int  i =1,  j =0,  k

//k=i/j; This sentence throws an exception that is displayed by 0 except

Try

{

< /span>k i / j // causes division-by-zero exception

throw new Exception ("hello.exception!");

}

Catch (arithmeticexception e) The catch type is an arithmetic exception, the capture object is E, and the arithmetic exception is, for example, 0 except for this case

{

< /span>system. out "by 0 except .  " e .getmessage ());   

E.getmessage () This sentence is a class in the package, which can be removed.

}

/*exception for all exception types. With a catch statement, you can only capture objects of the exception class and its subclasses.

Therefore, a catch statement block that captures the exception object captures all the "catch" exceptions. */

Catch (Exception e)

{

if (e instanceof arithmeticexception)

< /span>// instanceof for if E can be converted to arithmeticexception this type

< /span> out "by 0 except" );

Else

{

System. out . println (E.getMessage ());

}

}

finally //finally statement will be executed

{

joptionpane . Showconfirmdialog (null,"OK");

}

}

}

The purpose of exception handling is to provide different error coping strategies and means according to the actual situation, making the procedure more stable and safer.

The primary purpose of exception handling is to provide accurate error messages explaining the causes, location, and error types of the failure, while providing some resiliency to ensure that data integrity is not compromised as much as possible, and that the program can continue to run.

Exception capture statements in Java

All the exceptions that can be caught in Java derive from the Exception class.

Using the Java exception handling mechanism

* Put the code in the TRY statement block where the error may occur.

* An exception object is thrown when the program detects an error occurred. The exception handling code captures and handles this error. The code in the Catch statement block is used to handle errors.

* When an exception occurs, the program control process jumps from the TRY statement block to the catch statement block.

* The statements in the finally statement block are always guaranteed to be executed, regardless of whether an exception occurs.

* If no appropriate exception handling code is provided, the JVM will end the entire application.

Classes in the JDK that are related to exceptions

Classification of exceptions in Java

There are two direct subclasses of the Throwable class:

Exception: Problems that arise can be captured;

Error: System errors, usually handled by the JVM.

The catch exceptions can be divided into two categories:

(1) Check Exception: An exception class that derives directly from exception, must be caught or declared thrown

(2) Runtime exception: Exception class derived from RuntimeException. You can throw this exception object at any time using the Throw statement:

throw new ArithmeticException (...);

The "polymorphic" nature of the exception

* There can be more than one catch statement block, and each block of code captures an exception. It is a syntax error to have two different catch blocks after a try block to catch two of the same type of exception.

* With catch statements, you can only capture objects of the exception class and its subclasses. Therefore, a catch statement block that captures the exception object captures all the "catch" exceptions.

* Placing catch (Exception e) in front of other catch blocks causes these catch blocks to not execute, so Java does not compile the program.

The function of "finally"

* Resource disclosure: This happens when a resource is no longer being used by an application, but this program does not declare to the system that the resource is no longer being used

The *finally statement block is primarily used to resolve resource leaks, which are located after the catch statement block, and the JVM guarantees that they must execute.

* Note: An exception may also occur in the finally statement block, and if this happens, the previous exception is discarded.

The basics of implementing exception handling in Java

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.