Java notes: Basic concepts of exceptions

Source: Internet
Author: User
Tags throw exception throwable

An exception is a flow of instruction that causes a program to break.

Once an exception is generated, the statement following the exception does not execute, stays in the exception position, and reports the exception to the user.

Basic format:

try{

The statement to execute

}

catch () {

Throw exception

}

finally{

exception of the only exit

}

For example:

public class Exceptiontest {public static void main (String args[]) {int i = 10;int J =0;try {int temp = i/j; SYSTEM.OUT.PRINTLN (temp);} catch (Exception e) {//Todo:handle exceptionSystem.out.println ("Exception:" +e);} Finally{system.out.println ("Over");}}}


Inheritance structure of the exception class:

In Java's exception structure, the two most commonly used classes are exception and error, and these two classes are throwable subclasses.

Exception generally indicates that the error occurred in the program, can be solved by try...catch;

Error refers to a JVM error that cannot be handled in the program.


ThrowableA class is a superclass of all errors or exceptions in the Java language. A Java Virtual machine or Java statement can only be thrown if the object is an instance of this class (or one of its subclasses) throw . Similarly, only one of this class or its subclasses can be a catch parameter type in the clause.

You can use SYSTEM.OUT.PRINTLN () to print exceptions directly when handling exceptions, or you can use a method provided by exception: public void Printstacktrace ()

The process of handling exceptions in Java is as follows:

* Once an exception is generated, an instantiated object of the exception class is generated.

* Snap To this exception in a try statement

* The resulting exception object matches each exception type in the Catch statement and executes the code in the catch if the match succeeds

* If there is no successful match, the program interrupts execution (typically rare)



The exception types in catch can be written directly exception, depending on the output, but it is best to catch exceptions separately.

Exception statements that capture coarser particles are placed after the statement that captures the finer-grained exception, like water leaks, sand, rocks, leaks first, sand, and finally stones.


Throws keywords

When defining a method, you can use the throws keyword to illustrate that the method does not handle exceptions, but when the method is called, the exception is handled.

For example:

Class Math{public int div (int i,int j) throws Exception{int temp = I/j;return temp;}} public class Exceptiontest {public static void main (String args[]) {int a = 10;int b = 0; Math math = new math (); try {  //must use Try.catch to catch exception System.out.println (Math.div (A, B)),} catch (Exception e) {//TODO Auto -generated catch Blocke.printstacktrace ();}}}

If throws Exception is also used on the main method, the main method does not handle the exception, and the tool that handles the exception is the JVM.


throw keyword

Throw throws an exception in the program, and the exception it throws is an instantiated object of an exception class.

For example:

public class Exceptiontest {public static void main (String args[]) throws Exception {try {throw new Exception ("throws an exception");} catch (Exception e) {//Todo:handle exceptionSystem.out.println (e);}}}
In general practical development, throw and throws are used together. For example, to show all of the program execution process, but also to print out the relevant exceptions, it is necessary to give all the exceptions to the method call processing,

For example:

Class Math {public int div (int i, int j) throws Exception {System.out.println ("Start"); int temp = 0;try {temp = i/j;} CA TCH (Exception e) {//Todo:handle exceptionthrow E;} finally {System.out.println ("over");} return temp;}} public class Exceptiontest {public static void main (String args[]) {Math math = new Math (); try {System.out.println (math.di V (10, 0));} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}



You can also customize exception classes when using exceptions, and create classes that inherit the exception class.

The difference between exception and runtimeexception:

Exception must use Try...catch processing in the program;

RuntimeException can be processed without using try...catch, and if there is an exception, it is given to the JVM.



Java notes: Basic concepts of exceptions

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.