Some common runtimeException

xiaoxiao2021-03-06  55

Java adopted a mandatory abnormal capture mechanism, so as to improve the programs, but sometimes there are some troubles. For example: int i = integer.Parseint ("33"); this I know that it will not throw an exception, but if you don't capture an abnormality, you can't compile it. Of course this code is meaningless.

All exceptions in Java inherit throwable, we divide it into three categories: 1. Error: All inherited from Error, indicating fatal errors, such as inadequate memory, unhanect byte code. 2.Exception: This is an exception that is an application level, which must be captured. 3.RuntimeException: Strange RuntimeException inherits Exception, not directly Error, this means that the system is abnormal, more serious.

Error, we have few problems, but not that error must be very deadly, give an example, NosuchMethodError said there is no way, the method you call does not exist, you must feel weird, don't exist how to compile? Very simple, you should compile a class A that is called to give a method. Then call it in your program, compile, no problem, running is no problem. Now, remove this method in Class A, recompilate it, you will know what the error is going on again. Exception is not used, we have to capture yourself. RuntimeException can be seen, let's explain the common runtimeException:

NullPointersRexception: The most seen, is actually very simple, generally called the NULL object. String s = null; boolean EQ = S.Equals (""); // NullPointerexception Here you see very well, why is it in the program? Public int GETNUMBER (STRING STR) {IF (Str.Equals ("a")) Return 1; Else IF (Str.Equals ("B")) Return 2;} This method is likely to throw NullPointerexception, I suggest you Proactively throw an exception, because the code is more, you may faint. Public int GETNUMBER (STRING STR) {if (str == null) throw new nullpointRexception ("Parameter can't be empty"); // Do you think you understand more if (Str.Equals ("a")) Return 1; Else IF (str.equals ("b")) RETURN 2;}

Numberformatexception: Inherited IllegalarGumentException, the string is converted to a number. For example, Int i = integer.parseint ("ab3");

ArrayIndexOutofboundsexception: Array Boundary, int [] a = new int [3]; int b = a [3];

StringIndexoutofboundsexception: string of string, such as string s = "hello"; char c = s.chatat (6);

ClassCastException: Type conversion error, such as object obj = new object (); string s = (string) Obj; unsupportedOperationException: This operation is not supported, if we want to not support this method, you can throw this exception. Since I don't support it, do you want this? There is a possible subclass that does not want to support the method in the parent class, you can directly throw this exception.

ArithmeticException: Arithmetic error, typical is 0 as divisions.

IllegalarGumentException: Illegal parameters, an exception that frequently appears when converting strings into numbers, we can use this exception in our own procedure.

These exceptions know what is going on, in fact, as long as it understands the Java's exception handling mechanism, these are not a problem.

转载请注明原文地址:https://www.9cbs.com/read-114160.html

New Post(0)