Excption with the error packet structure. What situations have you encountered, SOF?

Source: Internet
Author: User
Tags stack trace

Java Exception schema diagram

1. Throwable
Throwable is a superclass of all errors or exceptions in the Java language.
Throwable consists of two subclasses: Error and Exception . They are typically used to indicate that an exception has occurred.
Throwable contains a snapshot of the thread execution stack at the time of its thread creation, which provides information such as Printstacktrace () for obtaining stack trace data.

2. Exception
Exception and its subclasses are a form of throwable that identifies the conditions that a reasonable application wants to capture.

3. RuntimeException
RuntimeException is a superclass of exceptions that might be thrown during the normal operation of a Java virtual machine.
The compiler does not check for runtimeexception exceptions. For example, a ArithmeticException exception is thrown when the divisor is zero. RuntimeException is a super class of ArithmeticException. When the code has a divisor of zero, if the "ArithmeticException exception is not thrown through the throws declaration", it "does not pass the Try...catch ... Handle the exception ", can also be compiled. This is what we call the "compiler does not check for runtimeexception exceptions"!
If the code produces a RuntimeException exception, it needs to be avoided by modifying the code. For example, if there is a divisor of zero, you need to avoid the situation by code!

4. Error
As with exception, error is also a subclass of Throwable. It is used to indicate that a reasonable application should not attempt to capture a serious problem, and most of these errors are exception conditions.
As with RuntimeException, the compiler does not check for error.

Java divides the throwable structure into three types: checked exceptions (Checked Exception), run-time exceptions (runtimeexception), and Errors (error).

(01) Run-time exception
definition : RuntimeException and its subclasses are called run-time exceptions.
feature : the Java compiler does not check it. That is, when such an exception can occur in a program, it is "not caught with the Try-catch statement" if it is "not thrown by the throws declaration", or it will be compiled. For example, a ArithmeticException exception that occurs when the divisor is zero, and an indexoutofboundsexception exception that occurs when the array is out of bounds, The concurrentmodificationexception exception generated by the Fail-fail mechanism belongs to the runtime exception.
Although the Java compiler does not check for runtime exceptions, we can either declare them by throws or capture them with Try-catch.
If you produce a run-time exception, you need to avoid it by modifying the code. For example, if there is a divisor of zero, you need to avoid the situation by code!

(02) Check for anomalies
definition : The exception class itself, and the subclasses of exception except for "Runtime exceptions", belong to the checked exception.
feature : the Java compiler will check it. Such exceptions, either declared by throws, or captured by Try-catch, cannot be compiled. For example, Clonenotsupportedexception belongs to a checked exception. When an object is cloned through the clone () interface, and the corresponding class of the object does not implement the Cloneable interface, the clonenotsupportedexception exception is thrown.
The exceptions that are checked are usually recoverable.

(03) Error
definition : The error class and its subclasses.
features : As with run-time exceptions, the compiler does not check for errors.
An error occurs when the resource is insufficient, the constraint fails, or if another program cannot continue to run. The program itself cannot fix these errors. For example, Virtualmachineerror is an error.
According to Java conventions, we are not supposed to be implementing any new error subclasses!

For the 3 structures above, what exactly do we do when we throw an exception or an error? The advice given in effective Java is to use a checked exception for a condition that can be recovered, and a run-time exception for program errors.

OOM:

1, OutOfMemoryError exception

In addition to the program counters, several other runtime areas of the virtual machine memory have the possibility of OutOfMemoryError (OOM) exceptions.

Java Heap Overflow

General exception information: Java.lang.OutOfMemoryError:Java heap Spacess

The Java heap is used to store object instances, as long as we constantly create objects, and ensure that GC roots to objects with an attainable path to avoid the garbage collection mechanism from clearing these objects, a memory overflow exception occurs after the number of objects reaches the maximum heap capacity limit.

In this case, the general approach is to first analyze the dump snapshot with a memory image analysis tool (such as Eclipse Memory Analyzer), with a focus on confirming that the object in memory is necessary, first distinguishing it from the memory leak Leak Memory Overflow.

In the case of a memory leak, further tools can be used to view the reference chain of the leaking object to the GC roots. It is then possible to find out what path is associated with GC roots when leaking objects and causes the garbage collector to not be automatically recycled.

If there are no leaks, you should check that the settings for the virtual machine's parameters (-XMX and-XMS) are appropriate.

2, virtual machine stack and local method stack Overflow

A Stackoverflowerror exception is thrown if the thread requests a stack depth that is greater than the maximum allowed depth for the virtual machine.

Throws a OutOfMemoryError exception if the virtual machine cannot request enough memory space in the expansion stack

It is important to note that the larger the stack size, the less the number of threads can be allocated.

3, run a constant pool overflow

Exception information: Java.lang.OutOfMemoryError:PermGen space

The simplest way to add content to a running constant pool is to use the String.intern () native method. The function of this method is that if the pool already contains a string equal to this string, a string object representing the string in the pool is returned, otherwise the string contained in this string object is added to the constant pool, and a reference to this string object is returned. Because the constant pool is allocated within the method area, we can limit the size of the method area through-xx:permsize and-xx:maxpermsize, thereby indirectly restricting the capacity of the constant pool.

4, Method area overflow

The method area is used to store information about class, such as its name, access modifier, Chang, field description, method description, and so on.

Exception information: Java.lang.OutOfMemoryError:PermGen space

Method area Overflow is also a common memory overflow exception, and a class is very demanding if it is to be reclaimed by a garbage collector. This is especially important in applications that generate a large number of classes on a dynamic basis.

Excption with the error packet structure. What situations have you encountered, SOF?

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.