JAVA基础--异常

异常的分类:

1. Throwable: 根类

  1) Error:系统错误, 由java虚拟机生成并抛出, 无法处理

  2) Exception: 所有异常类的父类, 可以处理的错误, 可以catch到

    1) RuntimeException:经常出现的错误, 特殊的异常, 比如被0除, 数组下标超范围等, 产生频繁, 处理麻烦, , 可以catch, 也可以不catch, 比如ArithmeticException,BufferOverflowExcetpion, IndexOutOfBoundsExcetpion

    2) 其他Exception: 必须要catch的错误, 如IOException

所以非RuntimeExcetpion必须要catch

异常的5个关键字: try, catch, finally, throws, throw

如何抛:

1. throws:已知错误类型

2. throw: 手动抛, 后面加异常对象

import java.io.*;

public class TestEx {
	public static void main(String[] args) {
		
		try {
			new TestEx().f2();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		/*
		int[] arr = {1, 2, 3};
		System.out.println(arr[2]);
		try {
			System.out.println(2/0);
		} catch (ArithmeticException e) {
			System.out.println("系统正在维护,请与管理员联系");
			e.printStackTrace();
		}
		*/
		
		//TestEx te = new TestEx();
		//te.m(0);
		
		/*
		try {
			new TestEx().m(0);
		} catch (ArithmeticException ae) {
			ae.printStackTrace();
			System.out.println("出错了");
		}
		*/
		
		
		FileInputStream in = null;
		
    try {
        in = new FileInputStream("myfile.txt");
        int b;
        b = in.read();
        while (b != -1) {
            System.out.print((char) b);
            b = in.read();
        }
    } catch (IOException e) {
      System.out.println(e.getMessage());
     	
    } catch (FileNotFoundException e) {
    	e.printStackTrace(); 
      
    } finally {
    	try {
      	in.close();
      } catch (IOException e) {
      	e.printStackTrace();
      }
    }
    
 
	}
	
	void m(int i) throws ArithmeticException {
		if(i==0) 
			throw new ArithmeticException("被除数为0");
	}
	
	void f() throws FileNotFoundException , IOException {
		FileInputStream in = new FileInputStream("myfile.txt");
    int b;
    b = in.read();
    while (b != -1) {
        System.out.print((char) b);
        b = in.read();
    }
	}
	
	void f2() throws IOException {
		/*
		try {
			f();
		} catch (FileNotFoundException e) {
			System.out.println(e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
		}
		*/
		f();
	}
	
}

 

try,catch,finally..

通常在finally语句中进行资源的清除工作, 比如关闭打开的文件, 删除临时文件.

FileInputStream in = null;
		
    try {
        in = new FileInputStream("myfile.txt");
        int b;
        b = in.read();
        while (b != -1) {
            System.out.print((char) b);
            b = in.read();
        }
    } catch (IOException e) {
      System.out.println(e.getMessage());
     	
    } catch (FileNotFoundException e) {
    	e.printStackTrace(); 
      
    } finally {
    	try {
      	in.close();
      } catch (IOException e) {
      	e.printStackTrace();
      }
    }
    

异常的捕获和处理:

方法持续往上抛异常, 用throws关键字

接收了以后, 在catch里必须处理.

void f() throws FileNotFoundException , IOException {
  FileInputStream in = new FileInputStream("myfile.txt");
    int b;
    b = in.read();
    while (b != -1) {
        System.out.print((char) b);
        b = in.read();
    }
}
	
void f2(){
  try {
	f();
  } catch (FileNotFoundException e) {
	System.out.println(e.getMessage());
  } catch (IOException e) {
	e.printStackTrace();
  }
  f();
}
	

也可以继续往上抛:

IOException包含了FileNotFoundException, 所以直接写IOException就可以了.

void f() throws FileNotFoundException , IOException {
  FileInputStream in = new FileInputStream("myfile.txt");
    int b;
    b = in.read();
    while (b != -1) {
        System.out.print((char) b);
        b = in.read();
    }
}
	
void f2() throws IOException {
  f();
}

  

再继续往上到main的时候要进行try catch处理:

public class TestEx {
	public static void main(String[] args) {
		
		try {
			new TestEx().f2();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
}

还可以不进行处理直接交给main, 但是不建议这样:

public class TestEx {
	public static void main(String[] args) throws Exception{
		new TestEx().f2();
    }
}

  

throw: 手动抛, 后面加异常对象:

void m(int i) throws ArithmeticException {
	if(i==0) 
	throw new ArithmeticException("被除数为0");
}

  

 

 

异常捕获时, 先捕获小的, 再捕获大的

重写方法需要抛出与原方法所抛出异常类型一致的异常, 或者不抛出异常.

自定义异常:

1. 通过继承Exception类声明自己的异常类

2. 在方法适当的位置生成自定义异常的实例, 并用throw抛出

3. 在方法的声明部分用throws语句声明该方法可能抛出的异常

class MyException extends Exception 
{
	private int id;
	public MyException(String message, int id){
		super(message);
		this.id = id;
	}
	public int getId(){
		return id;
	}
}
public class Test{
	public void regist(int num) throws MyException{
		if(num<0){
			throw new MyException("人数为负值,不合理",3);
		}
		System.out.println("登记人数 "+num);
	}
	public void manager(){
		try{
			regist(100);
		}
		catch (MyException e){
			System.out.println("登记失败, 出错类型码= "+e.getId());
			e.printStackTrace();
		}
		System.out.println("操作结束 ");
	}
	public static void main(String[] args) 
	{
		Test t = new Test();
		t.manager();		
	}
}

  

原文地址:https://www.cnblogs.com/wujixing/p/5329983.html