------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
异常:就是程序在运行时出现不正常的情况
异常由来:问题也是现实生活中一个具体的事物,也可以通过java的类的形式进行描述,并封装成对象。
其实就是java对不正常情况进行描述的对象体现
对于问题的划分分为两种:
1.一种是严重的问题
2.一种是非严重问题
对于严重的,java通过error类进行描述
对于error一般不变写针对性的代码进行处理
对于非严重的,java通过Exception进行描述
对于Exception可以使用针对性的处理方式进行处理
无论Error还是Exception都具有一些共性内容
比如:不正常情况信息,引发原因等
Throwable
|--Error
|--Exeption
java提供了特有的语句进行处理
try{
需要被检测到代码
}
catch(异常类 变量)
{
处理异常的代码;(处理方式)
}
finally
{
一定会执行的代码;
}
3.对于补获到的对象进行常见方法操作
String getMessage();获取异常信息
class Demo
{
int div(int a, int b) throws Exception//在功能上通过throws的关键字声明了该功能有可能会出现问题
{
return a/b;
}
}
class ExceptionDemo
{
public static void main(String args[]){
Demo d = new Demo();
try
{
int x = d.div(4,1);
System.out.println("x="+x);
}
catch(Exception e)
{
System.out.println("zero");
System.out.println(e.getMessage());
System.out.println(e.toString());//异常名称:异常信息
e.printStackTrace();//异常名称,异常信息,一场出现的位置
其实jvm默认的处理异常机制就是调用printStackTrace方法,打印异常的堆栈跟踪西信息
}
System.out.println("over");
}
}对捕获的异常进行常见方法操作class Demo
{
int div(int a, int b)throws ArithmeticException,ArryIndexOutOfBoundsException
//在功能上通过throws的关键字声明了该功能有可能会出现问题
{
int [] arr = new int[a];
System.out.println(arr[4]);
return a/b;
}
}
class ExceptionDemo
{
public static void main(String args[]){
Demo d = new Demo();
try{
int x = d.div(5,0);
System.out.println("x="+x);
}
catch(Exception e)
{
System.out.println(e.toString);
}
catch(ArithmeticException e)
{
System.out.println(e.toString);
System.out.println("by zero");
}
catch(ArrIndexOutOfboundsException)
{
System.out.println("e.toString);
System.out.println("角标越界");
}
System.out.println("over");
}
}
因为项目中会出现特有的问题<span style="font-family:KaiTi_GB2312;font-size:14px;">class fushuException exten</span>ds Exception
{
private int value;
fushuException()
{
super;
}
fushuException(String msg, int value)
{
super(msg);
this.value = value;
}
public int getValue()
{
return value;
}
}
class Demo
{
int div(int a, int b) throws fushuException//这个是throws
{
if(b<0)
throw new fushuException("出现了除数是负数的其概况",
b);//这个是throw
return a/b;
}
}
class ExceptionDemo3
{
public static void main(String args[])
{
Demo d = new Demo();
try
{
int x = d.div(4.-9);
System.out.println("x="+x);
}
catch(fushuException e)
{
System.out.println(e.toString());
System.out.println("错误的复数是:"+e.getVaule);
}
System.out.println("over");
}
}Exception中有一个特殊的子类异常RuntimeException运行异常class fushuException extends RuntimeException
{
fushuException(String msg)
{
super(msg);
}
}
class Demo
{
int div(int a, int b) throws Exception, throws ArithmeticException
{
if(b<0)
throw new Exception("出现负数了");
if(b==0)
throw new ArithmeException("被零除了");
return a/b;
}
}
class ExceptionDemo
{
public static void main(String args[]){
Demo d = new Demo();
int x = d.div(4, -9);
System.out.println("x="+x);
System.out.println("over");
}
}
/*
class Person
{
public void checkName(String name)
{
//if(name.equals("lisi"))//NullPointerException
if("lisi".equals(name))//if(name!=null && name.equals("lisi"))
System.out.println("YES");
else
System.out.println("no");
}
}
main()
{
Person p = new Person();
p.checkName(null);
}
*/
finally代码块:
/*
finally代码块:定义一定执行的代码。
通常用于关闭资源。
*/
class FuShuException extends Exception
{
FuShuException(String msg)
{
super(msg);
}
}
class Demo
{
int div(int a,int b)throws FuShuException
{
if(b<0)
throw new FuShuException("除数为负数");
return a/b;
}
}
class ExceptionDemo5
{
public static void main(String[] args)
{
Demo d = new Demo();
try
{
int x = d.div(4,-1);
System.out.println("x="+x);
}
catch (FuShuException e)
{
System.out.println(e.toString());
return;
//System.exit(0);//系统,退出。jvm结束。
}
finally
{
System.out.println("finally");//finally中存放的是一定会被执行的代码。
}
System.out.println("over");
}
}
class NoException extends Exception
{
}
public void method()throws NoException
{
连接数据库;
数据操作;//throw new SQLException();
关闭数据库;//该动作,无论数据操作是否成功,一定要关闭资源。
try
{
连接数据库;
数据操作;//throw new SQLException();
}
catch (SQLException e)
{
会对数据库进行异常处理;
throw new NoException();
}
finally
{
关闭数据库;
}
}
catch是用于处理异常。如果没有catch就代表异常没有被处理过,如果该异常是检测时异常。那么必须声明。
class Demo
{
public void method()
{
try
{
throw new Exception();//没有catch,必须声明异常
}
finally
{
//close
}
}
}
class
{
public static void main(String args[]){
System.out.println("hello world");
}
}异常在子父类覆盖中的体现;class Aexception extends Exception
{
}
class BException extends AException
{
}
class CException extends Exception
{
}
class fu
{
void show() throws AException
{
}
}
class test
{
void function(fu f)
{
try
{
f.show();
}
catch(AException e)
{
}
}
}
class zi extends fu
{
void show thros CException
{
}
}
class
{
public static void main(String args[]){
test t = new test();
t.function(new zi());
}
}
原文地址:http://blog.csdn.net/orangeisnotapple/article/details/41757967