一、异常
1、概念
异常是一种Exception类型的对象。 方法在运行期间遇到问题时抛出异常
2、语句结构
1)简单的异常声明
try {
} catch ( Exception ex) {
}
2)有风险会抛出异常的程序代码
概念
方法可以抓住其他方法抛出的异常,异常总是会丢回给调用方 会抛出异常的方法必须要声明它有可能会这么做 方法可以用throw关键词抛出异常对象:throw new FilesTooSmallException(); 可能抛出异常的方法必须声明成throws Exception
语句
public void takeRisk ( ) throws BadException{
if ( abandonAllHope) {
throw new BadException ( ) ;
}
}
public void crossFiguers ( ) {
try {
anObject. takeRisk ( ) ;
} catch ( BadException ex) {
System. out. println ( "Aaargh!" ) ;
ex. printStackTrace ( ) ;
}
}
3)try/catch块的流程控制
执行顺序
try {
Foo f= x. doRiskeyThing ( ) ;
int b= f. getNum ( ) ;
} catch ( Exception e) {
System. out. println ( "failed" ) ;
}
System. out. println ( "we made it!" ) ;
如果try中可能会出现异常的部分在执行过程中没有出现异常,那么按照语句顺序正常执行try中所有语句,catch部分不执行,直接跳到System.out.println(“we made it!”); -如果try中可能会出现异常的部分在执行过程中出现异常,那么执行在try中的部分立即停止,try中以后的部分不会执行,然后跳转到catch部分继续执行,最后接着执行System.out.println(“we made it!”);
Finally部分
在以上执行顺序中有一个System.out.println(“we made it!”); 部分,可以发现,无论异常是否出现,这一部分都是需要执行的,那么就可以用finally实现其表示
finally {
System. out. println ( "we made it!" ) ;
}
除了用finally表示必须要执行的部分以外,也可以在try和catch部分分别来说明必须要执行的语句,这样也可以实现无论异常是否出现,必要执行的部分都会被执行
执行顺序的例子
public class Example {
public static void main ( String[ ] args) {
String test= "no" ;
try {
System. out. println ( "Start try" ) ;
doRisky ( test) ;
System. out. println ( "end try" ) ;
} catch ( ScaryException se) {
System. out. println ( "scary exception" ) ;
} finally {
System. out. println ( "finally" ) ;
}
System. out. println ( "end of main" ) ;
}
static void doRisky ( String test) throws ScaryException{
System. out. println ( "start riskey" ) ;
if ( "yes" . equals ( test) ) {
throw new ScaryException ( ) ;
}
System. out. println ( "end risky" ) ;
return ;
}
public class ScaryException extends Exception {
}
public class Example {
public static void main ( String[ ] args) {
String test= "yes" ;
try {
System. out. println ( "Start try" ) ;
doRisky ( test) ;
System. out. println ( "end try" ) ;
} catch ( ScaryException se) {
System. out. println ( "scary exception" ) ;
} finally {
System. out. println ( "finally" ) ;
}
System. out. println ( "end of main" ) ;
}
static void doRisky ( String test) throws ScaryException{
System. out. println ( "start riskey" ) ;
if ( "yes" . equals ( test) ) {
throw new ScaryException ( ) ;
}
System. out. println ( "end risky" ) ;
return ;
}
}
public class ScaryException extends Exception {
}
4)多重异常
public class Laundry {
public void doLaundry ( ) throws PantsException, LingerieException{
}
}
public class Foo {
public void go ( ) {
Laundry laundry= new Laundry ( ) ;
try {
laundry. doLaundry ( ) ;
} catch ( PantsException pex) {
} catch ( LingerException lex) {
}
}
}
public class PantsException extends Exception {
}
public class LingerException extends Exception {
}
3、异常的多态
异常是对象,除了可以被抛出外,没有什么特别的地方 注意:存在多个catch块时,而且catch块之间存在着继承关系,那么就要先写比较小的,然后逐渐增大,到父类。例如从ShirtException到ClothingException
1)以异常的父型来声明会抛出的异常
在这种情况下,抛出的是父型异常,那么在catch的时候可以使用该父型异常的任何一种子类解决方案
public void doLaundry ( ) throws ClothingException{
}
try {
laundry. doLaundry ( ) ;
} catch ( PantsException pex) {
}
4.避免处理异常
1.
声明要抛出的异常,但是这个异常并没有try或者catch块来处理风险就是在避免处理异常 声明抛出异常,但没有try/catch块就会将异常留给调用方
public void doLaundry ( ) throws ClothingException{
}
2.main()避免处理异常
public class Washer {
Laundry laundry= new Laundry ( ) ;
public void foo ( ) throws ClothingException{
laundry. doLaundry ( ) ;
}
public static void main ( String[ ] args) throws ClothingException{
Washer a= new Washer ( ) ;
a. foo ( ) ;
}
}
public class ClothingException extends Exception {
}
5.异常处理规则
catch与finall不能没有try try与catch之间不能有程序 try一定要catch或者finally 只带了finally的try必须要声明异常