• Java异常的例子


    Java异常的例子 佟强 http://blog.csdn.net/microtong 2008.11.29

    对于检查型异常,Java强迫程序必须进行处理,处理的方式有以下两种:

      1.声明抛出异常:不在当前方法内处理异常,而是把异常抛出到调用方法中;

      2.捕获异常:使用try、catch和finally构成的语句块,捕获所发生的异常,进行相应的处理。

      3.除了使用系统预定义的异常类外,用户还可以声明自己的异常类。

    下面代码的输出是:

    before call f2()
    before call f3()
    before call f4()
    before call f5()
    before
    cn.edu.uibe.oop3.MyException: chu cuo la
     at cn.edu.uibe.oop3.ExpDemo2.f5(ExpDemo2.java:36)
     at cn.edu.uibe.oop3.ExpDemo2.f4(ExpDemo2.java:30)
     at cn.edu.uibe.oop3.ExpDemo2.f3(ExpDemo2.java:25)
     at cn.edu.uibe.oop3.ExpDemo2.f2(ExpDemo2.java:14)
     at cn.edu.uibe.oop3.ExpDemo2.f1(ExpDemo2.java:8)
     at cn.edu.uibe.oop3.ExpDemo2.main(ExpDemo2.java:46)
    chu cuo la
    Any Time
    after call f3()
    after call f2()

    1. package cn.edu.uibe.oop3;
    2. class MyException extends Exception{
    3.     MyException(){
    4.         super("myexception");
    5.     }
    6.     MyException(String msg){
    7.         super(msg);
    8.     }
    9. }
    10. public class ExpDemo2 {
    11.     public void f1(int n){
    12.         System.out.println("before call f2()");
    13.         f2(n);  
    14.         System.out.println("after call f2()");
    15.     }
    16.     public void f2(int n){
    17.         System.out.println("before call f3()");
    18.         try{
    19.             f3(n);
    20.         }catch(MyException e){
    21.             e.printStackTrace();
    22.             System.out.println(e.getMessage());
    23.         }finally{
    24.             System.out.println("Any Time");
    25.         }
    26.         System.out.println("after call f3()");
    27.     }
    28.     public void f3(int n)throws MyException{
    29.         System.out.println("before call f4()");
    30.         f4(n);
    31.         System.out.println("after call f4()");
    32.     }
    33.     public void f4(int n) throws MyException{
    34.         System.out.println("before call f5()");
    35.         f5(n);
    36.         System.out.println("after call f5()");
    37.     }
    38.     public void f5(int n) throws MyException{
    39.         System.out.println("before");
    40.         if(n<0){
    41.             throw new MyException("chu cuo la");
    42.             
    43.         }else{
    44.             System.out.println("success!");
    45.         }
    46.         System.out.println("after");
    47.     }
    48.     
    49.     public static void main(String[] args) {
    50.         ExpDemo2 ed = new ExpDemo2();
    51.         ed.f1(-100);
    52.     }
    53. }
  • 相关阅读:
    Android-----图片处理工具
    Android-----使用zxing实现二维码扫描
    Android-----调用系统相机拍照并把照片展示在图库中
    Android-----使用SoapObject获取服务器数据
    Android-----实现给图片添加字体
    Android-----WebView加载HTML界面布局并进行数据交互
    Android-----File(文件各种操作)
    Android-----创建SQLite数据库
    Android-----spinner组件使用(实现下单)
    SpringBoot学习日记1:HelloWorld
  • 原文地址:https://www.cnblogs.com/zhangyunlin/p/6168118.html
Copyright © 2020-2023  润新知