异常的分类:

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

  

JAVA基础--异常的更多相关文章

  1. Java基础-异常(Exception)处理

    Java基础-异常(Exception)处理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.异常的概述 什么是异常?Java代码在运行时期发生的问题就是异常.在Java中,把异 ...

  2. 《Java基础——异常的捕获与抛出》

    Java基础--异常的捕获与抛出     '  前言: Error类(错误)和Exception类(异常)是Throwable类的子类. 异常分为CheckedException类(编译时异常)和Ru ...

  3. Java基础 - 异常详解

    异常的层次结构 Throwable Throwable 是 Java 语言中所有错误与异常的超类. Throwable 包含两个子类:Error(错误)和 Exception(异常),它们通常用于指示 ...

  4. Java基础——异常体系

    在Java中,异常对象都是派生于Throwable类的一个实例,Java的异常体系如下图所示: 所有的异常都是由Throwable继承而来,在下一层立即分解为两个分支,Error和Exception. ...

  5. JAVA基础——异常详解

    JAVA异常与异常处理详解 一.异常简介 什么是异常? 异常就是有异于常态,和正常情况不一样,有错误出错.在java中,阻止当前方法或作用域的情况,称之为异常. java中异常的体系是怎么样的呢? 1 ...

  6. Java基础——异常

    一.什么是异常  异常的英文单词是exception,字面翻译就是“意外.例外”的意思,也就是非正常情况.事实上,异常本质上是程序上的错误,包括程序逻辑错误和系统错误.比如使用空的引用.数组下标越界. ...

  7. Java基础——异常机制

    [捕获异常] 硬件的错误.输入错误.物理限制等问题,都可能导致程序运行时的异常出现. 1.异常的分类层次 在java中,异常对象都是由Throwable类继承而来的,主要分为两大类: Error和Ex ...

  8. 【Demo 0009】Java基础-异常

    本章学习要点:       1.  了解异常的基本概念:       2.  掌握异常捕获方法以及注意事项;       3.  掌握异常抛出方法:       4.  掌握自定义异常类和异常类继承注 ...

  9. 面试题-Java基础-异常部分

    1.Java中的两种异常类型是什么?他们有什么区别? Java中有两种异常:受检查的(checked)异常和不受检查的(unchecked)异常.不受检查的异常不需要在方法或者是构造函数上声明,就算方 ...

随机推荐

  1. CentOS7 PostgreSQL 主从配置( 三)

    postgres 主备切换 主备查看 方法 ps -ef | grep wal (主库 sender)postgres 27873 27864 0 5月06 ? 00:00:10 postgres: ...

  2. login/logout切换

    1. 前端按钮 <img border="0" width="18" height="18" src="<%=base ...

  3. php常用命令大全

    1.php -v 查看版本号   [root@rs-2 lib]# php -v   PHP 5.5.11 (cli) (built: Apr 29 2014 12:35:52)   Copyrigh ...

  4. 二维 ST POJ 2019

    题目大意:给你一个n*n的矩阵,每次给你一个点(x,y),以其为左上角,宽度为b的矩阵中最小的数值和最大数值的差是多少?  一共k个询问. 思路:简单的二维st. 定义dp(i,j,k,L)表示以(i ...

  5. Commit message 和 Change log 编写指南

    来源:http://www.ruanyifeng.com/blog/2016/01/commit_message_change_log.html Git 每次提交代码,都要写 Commit messa ...

  6. photoshop移动工具

    1*移动工具 V  移动图层  若果移动选区相当于剪切 2*

  7. UITextfield的一些属性

    //设置左视图 不用设置frame UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@&quo ...

  8. Socks

    Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...

  9. 关于Arduino 步进电机Stepper库的一些想法

    官方提供了一些库,使Arduino入门起来更加快速,我们连原理都不用懂,就能通过函数控制终端.但是,这样也带来了很多的缺陷,比如,库函数的功能有限,有些无法实现.然后还有库函数因为要考虑其他的情况,你 ...

  10. USB鼠标线序

    鼠标线断了,找了个废弃的手机充电线接上,特记录线序如下: 红————白          白————橙绿————绿黑————蓝