search
HomeJavaJavaBaseSeveral common exceptions in java

Several common exceptions in java

Nov 13, 2019 pm 03:10 PM
javacommonabnormal

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());
}

The exception information output by the console is:

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

2.ClassCastException: Type cast exception

Example:

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

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)

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

Example:

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

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)

4.ArithmeticException : Arithmetic operation exception

Example:

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

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)

5.NumberFormatException: Number format exception

Example:

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

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)

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.