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();
}
}
JAVA基础--异常的更多相关文章
- Java基础-异常(Exception)处理
Java基础-异常(Exception)处理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.异常的概述 什么是异常?Java代码在运行时期发生的问题就是异常.在Java中,把异 ...
- 《Java基础——异常的捕获与抛出》
Java基础--异常的捕获与抛出 ' 前言: Error类(错误)和Exception类(异常)是Throwable类的子类. 异常分为CheckedException类(编译时异常)和Ru ...
- Java基础 - 异常详解
异常的层次结构 Throwable Throwable 是 Java 语言中所有错误与异常的超类. Throwable 包含两个子类:Error(错误)和 Exception(异常),它们通常用于指示 ...
- Java基础——异常体系
在Java中,异常对象都是派生于Throwable类的一个实例,Java的异常体系如下图所示: 所有的异常都是由Throwable继承而来,在下一层立即分解为两个分支,Error和Exception. ...
- JAVA基础——异常详解
JAVA异常与异常处理详解 一.异常简介 什么是异常? 异常就是有异于常态,和正常情况不一样,有错误出错.在java中,阻止当前方法或作用域的情况,称之为异常. java中异常的体系是怎么样的呢? 1 ...
- Java基础——异常
一.什么是异常 异常的英文单词是exception,字面翻译就是“意外.例外”的意思,也就是非正常情况.事实上,异常本质上是程序上的错误,包括程序逻辑错误和系统错误.比如使用空的引用.数组下标越界. ...
- Java基础——异常机制
[捕获异常] 硬件的错误.输入错误.物理限制等问题,都可能导致程序运行时的异常出现. 1.异常的分类层次 在java中,异常对象都是由Throwable类继承而来的,主要分为两大类: Error和Ex ...
- 【Demo 0009】Java基础-异常
本章学习要点: 1. 了解异常的基本概念: 2. 掌握异常捕获方法以及注意事项; 3. 掌握异常抛出方法: 4. 掌握自定义异常类和异常类继承注 ...
- 面试题-Java基础-异常部分
1.Java中的两种异常类型是什么?他们有什么区别? Java中有两种异常:受检查的(checked)异常和不受检查的(unchecked)异常.不受检查的异常不需要在方法或者是构造函数上声明,就算方 ...
随机推荐
- 【翻译】Longest Palindromic Substring 最长回文子串
原文地址: http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-i.html 转载请注明出处:http:// ...
- Java多态(一)
父类: public class Parent { public String name; private String pass; public void say1(AA aa){ System.o ...
- ccw-ide
有bug,会把working set弄乱,整理后能重启一次正常,再次重启又乱了.
- .net ToString()用法详解与格式说明
我们经常会遇到对时间进行转换,达到不同的显示效果,默认格式为:2006-6-6 14:33:34 如果要换成成200606,06-2006,2006-6-6或更多的格式该怎么办呢? 这里将要用到:Da ...
- The magic behind configure, make, make install
原文:https://robots.thoughtbot.com/the-magic-behind-configure-make-make-install#where-do-these-scripts ...
- table详解
1.tr 元素定义表格行,th 元素定义表头,td 元素定义表格单元. tr内是th还是td可由自己定义,th,td可存在于任一行,th与td的区别在与th字体更粗 2.定义一个table默认有bor ...
- NBUT 1120 线段树
input q n q行 F a b或者Q a b output face left face top face right 可以用map或者线段树做 //map #include<cstdio ...
- POJ 3468<线段树,区间add>
题目连接 //位运算 k<<1 相当于 k*2 k<<1|1 相当于 k*2+1 /* 修改区间内的值,并且维护区间和. 详见代码 */ #include<cstdio& ...
- jquery 控制文本输入的字数
<textarea maxlength="200" id="remarks" onkeyup="title_len();">&l ...
- Alamofire 框架浅析
下面是 Github 主页上对 Alamofire 的描述 Elegant HTTP Networking in Swift 为什么这次我选择阅读 Alamofire 的源代码而不是 AFNetwor ...