例子1
package com.atguigu.java;
public class ErrorTest {
public static void main(String[] args) {
//1.栈溢出 java.lang.StackOverflowError
//main(args);
//2.堆溢出 java.lang.OutOfMemoryError
Integer[] arr = new Integer[1024*1024*1024];
}
}
例子2
package com.atguigu.java;
import java.sql.Date;
import java.util.Scanner;
import org.junit.Test;
public class ExceptionTest {
//ArithmeticException 算术异常
@Test
public void test7(){
int a = 10;
int b = 0;
System.out.println(a/b);
}
//InputMismatchException 输入不匹配
@Test
public void test6(){
Scanner scanner =new Scanner(System.in);
int score = scanner.nextInt();
System.out.println(score);
}
//NumberFormatException 数字格式异常
@Test
public void test5(){
String str = "123";
str = "abc";
int num = Integer.parseInt(str);
}
//ClassCastException 类型转换异常
@Test
public void test4(){
Object obj = new Date(0);
String str = (String) obj;
}
//StringIndexOutBoundsExcepton 字符串角标越界
@Test
public void test3(){
String str = "abc";
System.out.println(str.charAt(3));
}
//ArrayIndexOutBoundsExcepton 数组角标越界
@Test
public void test2(){
int[] arr = new int[10];
System.out.println(arr[10]);
}
//NUllPointerException 空指针异常
@Test
public void test1(){
int[] arr = null;
System.out.println(arr[3]);
String str = "abc";
str=null;
System.out.println(str.charAt(0));
}
}
例子3
package com.atguigu.java;
import org.junit.Test;
public class ExceptionTest1 {
@Test
public void test1(){
String str = "123";
str = "abc";
try{
int num = Integer.parseInt(str);
}catch(NumberFormatException e){
// System.out.println("出现了数值转换异常了,不要着急.....");
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
例子4
package com.atguigu.java;
import org.junit.Test;
public class FinallyTest {
@Test
public void testMethod(){
int num = method();
System.out.println(num);
}
public int method(){
try{
int[] arr = new int[10];
System.out.println(arr[10]);
return 1; //这个方法要有返回值
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
return 2;//这个方法要有返回值
}finally{
System.out.println("我一定会被执行");
return 3;
}
}
@Test
public void test1(){
try{
int a=10;
int b=0;
System.out.println(a/b);
}catch(ArithmeticException e){
// e.printStackTrace();
//在异常中又出现了一个异常,则下来会执行finally中的语句
int[] arr = new int[10];
System.out.println(arr[10]);
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("我好帅啊~~");
}
}
}
例子5
package com.atguigu.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.junit.Test;
public class FinallyTest {
@Test
public void test2(){
FileInputStream fis = null;
try {
File file = new File("hello.txt");
fis = new FileInputStream(file);
int data= fis.read();
while(data!=-1){
System.out.println((char)data);
data = fis.read();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//try-catch-finally结构可以嵌套
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
例子6
package com.atguigu.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ExceptionTest2 {
public static void main(String[] args) {
try{
method2();
}catch(IOException e){
e.printStackTrace();
}
method3();
}
public static void method3(){
try{
method2();
}catch (IOException e){
e.printStackTrace();
}
}
public static void method2() throws IOException{
method1();
}
public static void method1() throws FileNotFoundException,IOException{
File file = new File("hello.txt");
FileInputStream fis = new FileInputStream(file);
int data= fis.read();
while(data!=-1){
System.out.println((char)data);
data = fis.read();
}
fis.close();
}
}
例子7
package com.atguigu.java;
import java.io.FileNotFoundException;
import java.io.IOException;
public class OverrideTest {
public static void main(String[] args) {
OverrideTest test = new OverrideTest();
test.display(new SubClass());
//test是调用子类,但是方法的形参是父类,这就体现了多态性,当然如果此时子类抛出了异常,父类必须罩得住,所以子类重写的方法抛出的异常类型必须小于父类的
}
public void display(SuperClass s){
try{
s.method();
}catch (IOException e){
e.printStackTrace();
}
}
}
//父类
class SuperClass{
public void method() throws IOException{
}
}
//子类重写的方法抛出的异常类型不大于父类被重写的方法抛出的异常类型
class SubClass extends SuperClass{
public void method() throws FileNotFoundException{
}
}
例子8
package com.atguigu.java2;
public class StudentTest {
public static void main(String[] args) {
try {
Student s = new Student();
s.regist(-10);
System.out.println(s);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
class Student{
int id;
public void regist(int id) throws Exception{
if(id>0){
this.id=id;
}else{
//手动抛出异常对象
//throw new RuntimeException("您输入的数据非法");
throw new Exception("您输入的数据非法!");
}
}
public String toString() {
return "Student [id=" + id + "]";
}
}
例子9
package com.atguigu.java2;
public class MyException extends Exception{
static final long serialVersionUID = -7034897190745766939L;
public MyException(){
}
public MyException(String msg){
super(msg);
}
}
package com.atguigu.java2;
public class StudentTest {
public static void main(String[] args) {
try {
Student s = new Student();
s.regist(-10); //这里肯定出现问题
System.out.println(s);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
class Student{ //这是一个student类
int id; //id
public void regist(int id) throws Exception{ //类中定义的方法regist
if(id>0){
this.id=id;
}else{
//手动抛出异常对象
//throw new RuntimeException("您输入的数据非法");
throw new MyException("不能输入负数");
}
}
public String toString() {
return "Student [id=" + id + "]";
}
}