异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的。(语法错误和逻辑错误不是异常)
比如说,你的代码少了一个分号,那么运行出来结果是提示是错误 java.lang.Error;如果你用System.out.println(11/0),那么你是因为你用0做了除数,会抛出 java.lang.ArithmeticException 的异常。
异常发生的原因有很多,通常包含以下几大类:
用户输入了非法数据。要打开的文件不存在。网络通信时连接中断,或者JVM内存溢出。
这些异常有的是因为用户错误引起,有的是程序错误引起的,还有其它一些是因为物理错误引起的。
Error:Java虚拟机无法解决的严重问题。如:JVM系统内部错误、资源耗尽等严重情况。比如:StackOverflowError和OOM。一般不编写针对性代码进行处理。
Exception:其它因编程错误或偶然的外在因素导致的一般性问题,可以使用针对性的代码进行处理。如:空指针访问、试图读取不存在的文件、网络连接中断、数组角标越界。
分类检查性异常:最具代表性的检查性异常是用户错误或问题引起的异常,这是程序员无法预见的。例如要打开一个不存在的文件时,一个异常就发生了,这些异常再编译时不能简单地被忽略。运行时异常:运行时异常是可能被程序员避免的异常。与检查性异常相反,运行时异常可以在编译时被忽略。错误:错误不是异常,而是脱离程序员控制的问题。错误在代码中通常被忽略。例如,当栈溢出时,一个错误就发生了,它们在编译时也是检查不到的。 异常处理机制
抛出异常捕获异常 异常处理五个关键字
trycatchfinallythrowthrows 异常处理方式
try-catch-finally:
public class Test {
public static void main(String[] args) {
int a = 1;
int b = 0;
try {//监控区域
System.out.println(a/b);
}catch (Error e){//catch(想要捕获的异常类型) 捕获异常
System.out.println("程序出现异常,变量b不能为0");
}catch (Exception e){
System.out.println("exception");
}catch (Throwable e){
System.out.println("throwable");
}
finally {//可以没有finally
System.out.println("finally");
}
}
}
throws:
将异常抛出交给调用它的上级方法处理。写在方法的声明处,指明此方法执行时,可能会抛出的异常类型。一旦当方法体执行时,出现异常,仍会在异常代码处生成一个异常类的对象,此对象满足throws后异常类型时,就会被抛出。后续代码不再执行。
public class ExceptionTest2 {
public static void main(String[] args){
try {
method2();
}catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
method3();
}
public static void method3(){
try {
method2();
}catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void method2() throws FileNotFoundException, 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();
}
}
try-catch-finally:真正的将异常处理掉了
throws:只是将异常抛给了方法的调用者。并没有真正将异常处理掉。
开发中如何选择使用try-catch-finally还是throws?
如果父类中被重写的方法没有throws方式处理异常,则子类重写的方法也不能使用throws,意味着如果子类重写的方法中有异常,必须使用try-catch-finally方式处理。执行的方法a中先后又调用了另外的几个方法,这几个方法是递进关系执行的。我们建议这几个方法使用throws的方式进行处理。而执行的的方法a可以考虑使用try-catch-finally方式进行处理。 手动抛出异常throw
package com.atm.java;
public class StudentTest {
public static void main(String[] args) {
Student s = new Student();
try {
s.regis(-1001);
System.out.println(s);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
}
}
class Student{
private int id;
public void regis(int id) throws Exception{
if(id > 0){
this.id = id;
}else{
//System.out.println("您输入的数据非法");
//手动抛出异常
//方法1
//throw new RuntimeException("您输入的数据非法");
//方法2
throw new Exception("您输入的数据非法");
}
}
@Override
public String toString() {
return "Student [id=" + id + "]";
}
}
用户自定义异常
public class MyException extends RuntimeException{
static final long serialVersionUID = -7034897190745766939L;
public MyException(){
}
public MyException(String msg){
super(msg);
}
}
public class StudentTest {
public static void main(String[] args) {
Student s = new Student();
try {
s.regis(-1001);
System.out.println(s);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
}
}
class Student{
private int id;
public void regis(int id) throws Exception{
if(id > 0){
this.id = id;
}else{
//System.out.println("您输入的数据非法");
//手动抛出异常
//方法1
//throw new RuntimeException("您输入的数据非法");
//方法2
//throw new Exception("您输入的数据非法");
//方法3
throw new MyException("不能输入负数");
}
}
@Override
public String toString() {
return "Student [id=" + id + "]";
}
}



