finally中存放的是一定会被执行的代码,通常用于关闭资源。
class FuShuException1 extends Exception
{
FuShuException1(String msg)
{
super(msg);
}
}
class Demo4{
int div(int a,int b) throws FuShuException1
{
if (b<0)
{
throw new FuShuException1("除数为负数");
}
return a/b;
}
}
public class ExceptionDemo4 {
public static void main(String[] args) {
Demo4 d = new Demo4();
try{
d.div(2,-2);
}catch (FuShuException1 e)
{
System.out.println(e.toString());
}
finally {
System.out.println("finally");
}
System.out.println("over");
}
}
public void method() throws NoException
{
连接数据库;
数据操作;//throw new SQLException();
关闭数据库;//该动作,无论数据操作是否成功,一定要关闭资源。
try
{
连接数据库;
数据操作;//throw new SQLException();
}
catch(SQLException e)
{
会对数据库进行异常处理;
throw new NoException();
}
finally
{
关闭数据库;
}
}
语句格式
try
{
}catch
{
}
try
{
}catch
{
}
finally
{
}
try
{
}
finally
{
}
记住一点:catch是用于处理异常。如果没有catch就代表异常没有被处理过,如果该异常是检测时异常,那么必须声明。
异常在子父类覆盖中的体现;
1.子类在覆盖父类时,如果父类的方法抛出异常,那么子类的覆盖方法,只能抛出父类的异常或者该异常的子类。
2.如果父类方法抛出多个异常,那么子类在覆盖该方法时,只能抛出父类异常的子集。
3.如果父类或者接口的方法中没有异常抛出,那么子类在覆盖方法时,也不可以抛出异常。如果子类方法发生了异常。就必须要进行try处理。绝对不能抛。
class AException extends Exception
{
}
class BException extends Exception
{
}
class CException extends Exception
{
}
class Fu0
{
void show() throws AException
{
}
}
class Zi0 extends Fu0
{
void show() throws AException
{
}
}
class Test0
{
void function(Fu0 f)
{
try{
f.show();
}catch (AException e)
{
System.out.println(e.toString());
}
}
}
public class ExceptionDemo6 {
public static void main(String[] args) {
Test0 t = new Test0();
t.function(new Fu0());
}
}
//编译不报错
/*
Exception
|--AException
|--BException
|--CException
*/
class AException extends Exception
{
}
class BException extends Exception
{
}
class CException extends Exception
{
}
class Fu0
{
void show() throws AException
{
}
}
class Zi0 extends Fu0
{
void show() throws CException
{
}
}
class Test0
{
void function(Fu0 f)
{
try{
f.show();
}catch (AException e)
{
System.out.println(e.toString());
}
}
}
public class ExceptionDemo6 {
public static void main(String[] args) {
Test0 t = new Test0();
t.function(new Fu0());
}
}
//编译失败
Error:(22, 10) java: Zi0中的show()无法覆盖Fu0中的show()
被覆盖的方法未抛出CException
例子:面积为负数。
class NoValueException extends Exception
{
NoValueException(String message)
{
super(message);
}
}
interface Shape
{
void getArea();
}
class Rec implements Shape
{
private int len,wid;
Rec(int len,int wid) throws NoValueException
{
if (len<=0 || wid <=0)
throw new NoValueException("出现非法值!!");
else {
this.len = len;
this.wid = wid;
}
}
public void getArea()
{
System.out.println(len*wid);
}
}
class Circle implements Shape
{
public static final double PI = 3.14;
private double radius;
Circle(double radius) throws NoValueException
{
if (radius <= 0)
throw new NoValueException("出现非法值!!");
this.radius = radius;
}
public void getArea()
{
System.out.println(radius*radius);
}
}
public class ExceptionDemo7 {
public static void main(String[] args) {
try
{
Rec r = new Rec(-3,4);
r.getArea();
}
catch (NoValueException e)
{
System.out.println(e.toString());
}
System.out.println("over");
}
}
//输出
NoValueException: 出现非法值!!
over
class NoValueException extends RuntimeException
{
NoValueException(String message)
{
super(message);
}
}
interface Shape
{
void getArea();
}
class Rec implements Shape
{
private int len,wid;
Rec(int len,int wid) //throws NoValueException
{
if (len<=0 || wid <=0)
throw new NoValueException("出现非法值!!");
else {
this.len = len;
this.wid = wid;
}
}
public void getArea()
{
System.out.println(len*wid);
}
}
class Circle implements Shape
{
public static final double PI = 3.14;
private double radius;
Circle(double radius) throws NoValueException
{
if (radius <= 0)
throw new NoValueException("出现非法值!!");
this.radius = radius;
}
public void getArea()
{
System.out.println(radius*radius);
}
}
public class ExceptionDemo7 {
public static void main(String[] args) {
Rec r = new Rec(-3,4);
r.getArea();
System.out.println("over");
}
}
//程序结束
Exception in thread "main" NoValueException: 出现非法值!!
at Rec.<init>(ExceptionDemo7.java:18)
at ExceptionDemo7.main(ExceptionDemo7.java:47)
class NoValueException extends Exception
{
NoValueException(String message)
{
super(message);
}
}
interface Shape
{
void getArea();
}
class Rec implements Shape
{
private int len,wid;
Rec(int len,int wid) throws NoValueException
{
if (len<=0 || wid <=0)
throw new NoValueException("出现非法值!!");
this.len = len;
this.wid = wid;
}
public void getArea()
{
System.out.println(len*wid);
}
}
class Circle implements Shape
{
public static final double PI = 3.14;
private double radius;
Circle(double radius) throws NoValueException
{
if (radius <= 0)
throw new NoValueException("出现非法值!!");
this.radius = radius;
}
public void getArea()
{
System.out.println(radius*radius);
}
}
public class ExceptionDemo7 {
public static void main(String[] args) {
try
{
Rec r = new Rec(-3,4);
r.getArea();
}catch (NoValueException e)
{
System.out.println(e.toString());
}
System.out.println("over");
try
{
Circle c = new Circle(-2.68);
c.getArea();
}
catch (NoValueException e)
{
System.out.println(e.toString());
}
System.out.println("over");
}
}
//输出
NoValueException: 出现非法值!!
over
NoValueException: 出现非法值!!
over