JAVASE学习笔记 (十)(异常)
发布日期:2021-05-14 17:01:34 浏览次数:17 分类:精选文章

本文共 4365 字,大约阅读时间需要 14 分钟。

Java������������������

������������������

Java������������������������������������������������Throwable���������������������������������Java API������������������������������������������������������������������������

Error

���Java���������������������������������������������������������������������������������������������������Error���������

  • ���������������������OutOfMemoryError���VirtualMachineError���������������JVM���������������������
  • ���������������������NoClassDefFoundError���LinkageError������������������������������������������

Exception

������������������������������������������������������������RuntimeException������������������������������

  • ArrayIndexOutOfBoundsException������������������
  • NullPointerException���������������������
  • ArithmeticException������������������������
  • MissingResourceException������������������
  • ClassNotFoundException������������������
    ������������������������������������������Unchecked Exception������������������������������������������������������

Error���Exception���������

  • Error���������������������������������������JVM������������������������
  • Exception������������������������������������������������������������������������������������������������������������������

������������������

������������������������������������������������������

  • try������������������������������������������������
  • catch���������������������������������������������������
  • finally���������������������������������������������������
  • throw������������������������������
  • throws���������������������������������������������������������������

throw���throws���������

  • throw���������������������������������������������JVM���������
  • throws������������������������������������������������������������������

������Error

Error���������JVM������������������������������������������������������������������

���������������

public class StackOverflow {
public static void main(String[] args) {
new StackOverflow().test();
}
private void test() {
try {
test();
} catch (StackOverflowError e) {
System.out.println("���������");
}
}
}

���������������StackOverflowError: Stack overflow

���������������������������������

  • ���������������������������������������������������������������throws���������
  • ������������������������������������������������������������������RuntimeException���

������������������������

public class CustomeException {
public static void main(String[] args) {
try {
int result = m1(999);
System.out.println("result: " + result);
} catch (MyException e) {
System.out.println("���������������������");
}
}
public static int m1(int num) {
if (num % 2 == 0) {
throw new MyException("���������������");
}
return num + 100;
}
}
class MyException extends RuntimeException {
public MyException(String message) {
super(message);
}
}

������������������

public class Demo09 {
public static void main(String[] args) {
try {
int num = new Random().nextInt(10);
if (num < 4) {
throw new NullPointerException("������������");
}
System.out.println("������������");
} catch (NullPointerException e) {
System.out.println("���������������");
}
}
}

���������������

class MyException extends RuntimeException {
public MyException(String message) {
super(message);
}
}
public class Demo12 {
public static void main(String[] args) {
try {
int num = new Scanner(System.in).nextInt();
isEven(num);
System.out.println("������");
} catch (MyException e) {
System.out.println(e.getMessage());
}
}
public static void isEven(int num) throws MyException {
if (num % 2 == 0) {
throw new MyException("������");
}
}
}

������

  • ���������������������������������������������������
  • ������catch������������������������������
  • ** finally������������������**������������ releasing.
  • ������������������������������������������������������������
  • ���������������������������������������������������������������.

������������printStackTrace()���������������������������������������������������������������������������������������������������

上一篇:中国象棋小项目
下一篇:JAVASE的学习笔记(九)(Properties类和面向接口编程)

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2025年04月19日 11时13分58秒