Problem description
RuntimeException refers to those exceptions that cannot be predicted by the Java compiler. RuntimeException includes many categories. Only the following common categories are listed in this topic:
BufferOverflowException occurs if the buffer limit is reached when the Put operation is performed.
ArrayStoreException occurs when the type of the object that you want to store to an array is not supported.
ArithmeticException occurs when invalid operation conditions are specified. For example, if you specify that an integer is divided by zero, ArithmeticException occurs.
BufferUnderflowException occurs if the buffer limit is not reached when the Get operation is performed.
IndexOutOfBoundsException occurs if that a referenced sort index (such as sorting of arrays, strings, or vectors) exceeds the specified threshold.
NoSuchElementException is thrown by the nextElement method to indicate that the enumeration contains no more elements.
Solution
RuntimeException cannot be predicted by the Java compiler. The compiler does not require this exception to be caught. However, if RuntimeException is not handled during runtime, it will be thrown to the upper layer all the way to JVM. JVM generates the stack information and terminates the app. We recommend that you check whether the code contains any error and make changes based on the stack information. If the exception cannot be solved this way, you can catch the exception and handle it.
Sample code
try {
somethingThrowingARuntimeException();
}catch (RuntimeException e) {
// Do something with it. At least log it
e.printStackTrace();
}Example 1
java.lang.RuntimeException:Unable to start activity ComponentInfo{*}: android.support.v4.app.Fragment$InstantiationException:*: make sure class name exists,ispublic,and has an empty constructor that ispublicWhen the system kills a process which is not in the foreground due to insufficient memory, you bring it back to the foreground again. If fragments are used in the UI and the restore() method is invoked, the fragments need to reflect the instance object and the previously saved state is restored. Therefore, you cannot rewrite the constructor function, but use a static method such as newInstance to construct parameterized fragment objects.
Sample code:
1. Write a function to create parameterized objects by using a static method.
public static final AlertFragment newInstance(int title, String message)
{
AlertFragment f = new AlertFragment();
Bundle bdl = new Bundle(2);
bdl.putInt(EXTRA_TITLE, title);
bdl.putString(EXTRA_MESSAGE, message);
f.setArguments(bdl);
return f;
}2. Obtain parameters by using the bundle class.
@Override
public void onCreate(Bundle savedInstanceState)
{
title = getArguments().getInt(EXTRA_TITLE);
message = getArguments().getString(EXTRA_MESSAGE);
//...
//etc
//...
}3. Manage fragments by using FragmentManager.
public onCreate(Bundle savedInstanceState) {
if(savedInstanceState == null){
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content,AlertFragment.newInstance(
R.string.alert_title,
"Oh noes an error occured!")
)
.commit();
}
}Example 2
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 80, Size: 3
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.remove(ArrayList.java:474)
at com.alibaba.mqc.test.Test.indexOutOfBounds(Test.java:66)
at com.alibaba.mqc.test.Test.main(Test.java:32)Problem description:
IndexOutOfBoundsException occurs if that a referenced sort index (such as sorting of arrays, strings, or vectors) exceeds the specified threshold. Check whether referenced indexes for list objects exceed the specified threshold and whether indexes to access arrays are valid.
Error code:
public void indexOutOfBounds(){
ArrayList<Character> a = new ArrayList<Character>();
a.add('A');
a.add('B');
a.add('C');
System.out.println(a);
//a.remove('A');
A. remove(80); // Throw the exception.
System.out.println(a);
}Recommended code:
public void indexOutOfBounds(){
ArrayList<Character> a = new ArrayList<Character>();
a.add('A');
a.add('B');
a.add('C');
System.out.println(a);
if(a.size()>80){
a.remove(80);
}
System.out.println(a);
}References
https://developer.android.com/reference/java/lang/RuntimeException.html
http://stackoverflow.com/questions/10450348/do-fragments-really-need-an-empty-constructor
http://stackoverflow.com/questions/2028719/handling-runtimeexceptions-in-java
http://tool.oschina.net/uploads/apidocs/jdk-zh/java/lang/RuntimeException.html