Home > Java > JavaBase > body text

Several common exceptions in java

王林
Release: 2019-11-13 15:10:12
Original
1932 people have browsed it

Several common exceptions in java

##1.NullPointerException: Null pointer exception

Example:

public static void main(String[] args) {
String str = null;
//此处报空指针异常
System.out.println(str.length());
}
Copy after login

The exception information output by the console is:

Exception in thread "main" java.lang.NullPointerException
at cn.com.gjw.MyClass.main(MyClass.java:7)
Copy after login

2.ClassCastException: Type cast exception

Example:

public static void main(String[] args) {
// 类型强制转换异常
Object x = new String("String");
System.out.println((Integer) x);
}
Copy after login

The exception information output by the console is:

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at cn.com.gjw.MyClass.main(MyClass.java:7)
Copy after login

3.ArrayIndexOutOfBoundsException: Array subscript out-of-bounds exception

Example:

public static void main(String[] args) {
int arr[] = {1,2};
// 此处报数组下标越界异常
System.out.println(arr[2]);
}
Copy after login

The exception information output by the console is:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at cn.com.gjw.MyClass.main(MyClass.java:7)
Copy after login

4.ArithmeticException : Arithmetic operation exception

Example:

public static void main(String[] args) {
// 整数0做了分母,报算术运算异常
System.out.println(1 / 0);
}
Copy after login

The exception information output by the console is:

Exception in thread "main" java.lang.ArithmeticException: / by zero
at cn.com.gjw.MyClass.main(MyClass.java:6)
Copy after login

5.NumberFormatException: Number format exception

Example:

public static void main(String[] args) {
// 将字符串“it”转换为Integer类型的,当然会报数字格式异常啦
System.out.println(Integer.parseInt("it"));
}
Copy after login

The exception information output by the console is:

Exception in thread "main" java.lang.NumberFormatException: For input string: "it"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at cn.com.gjw.MyClass.main(MyClass.java:6)
Copy after login

Recommended tutorial:

Java tutorial

The above is the detailed content of Several common exceptions in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!