Full description of the exception mechanism in Java

Source: Internet
Author: User

Java Exception Summary:

An exception is when the program is running in an unhealthy state

1. The origin of the anomaly:

A description of the problem in the real thing in the form of a Java class, and it is sealed into an object

In fact, Java is the object of the description after the abnormal situation

2. There are two types of problems: one is serious, the other is a non-serious problem.

For serious, Java is described by the error class

For error It is generally not written with specific code to handle it

For non-critical, Java is described by the exception class

For exception can be handled with a targeted approach

3. Common exceptions are: array angle label out of bounds exception, NULL pointer exception ...

4. Both error and exception have some common content.

For example: abnormal information, cause of the causes and so on.

Throwable//Parent class

|--error

|--excption//Two subcategories (there are many problems defined (exceptions appear))

Example 1:

1 classDemo2 {3      Public intDivintXinty)4     {5         returnx/y;6     }7 }8 9 Ten classExceptiondemo One { A      Public Static voidMain (String args[]) -     { -Demo d=NewDemo (); the         intX=d.div (4,0); 0 as Divisor -System.out.println ("x=" +x); -System.out.println ("Over"); -     } +}

Operation Result:

Exception in thread "main" java.lang.ArithmeticException:/By zero
At Demo.div (exceptiondemo.java:5)
At Exceptiondemo.main (exceptiondemo.java:15)
From the above results can be analyzed, in the 5th and 15th lines have an exception, this is because of the division mechanism, the divisor cannot be 0, this time the run throws an exception.



Example 2:

1 classDemo2 {3      Public intDivintXinty)4     {5         returnx/y;6     }7 }8 9 Ten classExceptiondemo One { A      Public Static voidMain (String args[]) -     { -         /*Demo D=new demo (); the int X=d.div (4,0); - System.out.println ("x=" +x); - System.out.println ("over"); -         */ +         byte[] arr=New byte[1024*1024*1000]; -     } +}

Operation Result:

Exception in thread "main" Java.lang.OutOfMemoryError:Java heap space
At Exceptiondemo.main (exceptiondemo.java:19)

Java.lang.OutOfMemoryError: represents a memory overflow exception

Full description of the exception mechanism 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.