java异常处理机制

异常

/*
1.异常是什么?
第一,异常模拟的是现实世界中“不正常”的事件;
第二,java中采用“类”去模拟异常;
第三,类是可以创建对象的。
NullPointerException e = 0x1234;
e是引用类型,e所保存的地址指向内存中堆的“对象”;
这个对象是NullPointerException类型;
这个对象表示真实的异常事件;
NullPointerException是一类异常。
2.异常的作用是什么?
java语言提供异常处理机制,使程序更加健壮。
异常对象携带的信息显示出来,我们可以从异常对象中取出信息。

*/
public class ExceptionTest01{

public static void main(String[] args){
	int a=10;
	int b=0;
	int c=a/b;//Exception in thread "main" java.lang.ArithmeticException: / by zero
				//at ExceptionTest01.main(ExceptionTest01.java:19)
	System.out.println(c);//
	
}

}

异常的继承结构图

异常处理的两种方法
/*
处理异常有两种方式:
1.声明抛出 throw
2.捕捉 try...catch...
下面演示第一种。
*/

import java.io.*;

public class ExceptionTest02{
public static void main(String[] args) throws FileNotFoundException
{
FileInputStream fis =new FileInputStream("c:/a.txt");
/*
ExceptionTest02.java:17: 错误: 未报告的异常错误FileNotFoundException; 必须对其进
行捕获或声明以便抛出.
*/

}

}

深入throws
/*
深入throws

使用throws处理异常不是真正处理异常而是推卸责任,他是往上一级抛的方式。

/
import java.io.
;

public class ExceptionTest03{
public static void main(String[] args) throws FileNotFoundException{
m1();
System.out.println("hello world!");
}
public static void m1() throws FileNotFoundException{
m2();
}
public static void m2() throws FileNotFoundException{
FileInputStream fis=new FileInputStream("c:/a.txt");
}

}

/*
以上程序在运行时出现以下异常:
Exception in thread "main" java.io.FileNotFoundException: c:\a.txt (系统找不到指
定的文件。)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.(FileInputStream.java:138)
at java.io.FileInputStream.(FileInputStream.java:93)
at ExceptionTest03.m2(ExceptionTest03.java:17)
at ExceptionTest03.m1(ExceptionTest03.java:14)
at ExceptionTest03.main(ExceptionTest03.java:10)

*/

异常处理第二种方式:捕捉
/*
处理异常的第二种方法:捕捉
try{
可能发生异常的代码;
}
catch(异常类 变量){
异常处理语句;
}

catch方法执行,jrm就创建异常类对象,这是java运行机制。

/
import java.io.
;

public class ExceptionTest04{
public static void main(String[] args){

	/*
	这段代码不能通过编译,原因是指定的编译无法通过。
	ExceptionTest04.java:19: 错误: 未报告的异常错误FileNotFoundException; 必须对其进

行捕获或声明以便抛出;

	try{
		FileInputStream fis = new FileInputStream("c:/c.txt");
		
	}catch(ArithmeticException e){
		
	}
	*/
	/*
	这段代码不能通过编译,还有未处理的异常:IOException异常。
	ExceptionTest04.java:19: 错误: 未报告的异常错误FileNotFoundException; 必须对其进

行捕获或声明以便抛出;
try{
FileInputStream fis = new FileInputStream("abv");
fis.read();

	}catch(FileNotFoundException e){
		
	}*/
	
	/*
	以下代码可以通过编译,IOException是FileNotFoundException的父类。
	try{
		FileInputStream fis = new FileInputStream("aaa");
		fis.read();
	}catch(IOException e)
	{
		
	}
	*/
	//以下是正确的代码写法,如此便将异常安照“从小到大”的顺序进行处理。
	//只要有一个异常处理块得到处理,程序就向下执行,不在处理。如下面的代码“hello word!”就输不出来。
	try{
		FileInputStream fis = new FileInputStream("asdf");
		fis.read();
		System.out.println("hello world!");
	}
	catch(FileNotFoundException e)
	{
		
	}catch(IOException e)
	{
		
	}
	System.out.println("异常处理后直接输出");
}

}
自定义异常
/*
自定义异常:
1.编译时异常继承自Exception类;;
2.运行时异常继承自RuntimeException类;
3.一般自定义异常写两个构造函数:一个是无参构造函数,一个是有参数构造函数;
/
/

下面的异常定义了一个IllegalNameException
*/
public class IllegalNameException extends Exception{

public	IllegalNameException(){};

public	IllegalNameException(String msg)
{
	super(msg);
}	

}

/*
下面的代码使用了IllegalNameException
*/

import java.io.*;
public class ExceptionTest05{

public static void main(String[] args){
	String name="cjs";
	
	Login lg=new Login();
	try{
		lg.m1(name);
	}
	catch(IllegalNameException e)
	{
		String msg = e.getMessage();
		System.out.println(msg);
	}
	
}

}

/*

登录类:

*/
下面是测试程序:
public class Login{
public void m1(String name) throws IllegalNameException{
if(name.length()<=4)
{

		throw new IllegalNameException("输入的用户名称长度不够");
	}
	System.out.println("注册成功");
	
}

}

posted @ 2017-05-07 22:49  瀚海悠悠  阅读(184)  评论(0)    收藏  举报