国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

JAVA 處理程序異常,(try、catch、finally),(thorws)

2019-11-15 00:27:20
字體:
來源:轉載
供稿:網友
java 處理程序異常,(try、catch、finally),(thorws) Posted on 2015-06-04 23:18 楊波php 閱讀(...) 評論(...) 編輯 收藏

一、try語句:

try{//這里寫可能出現異常的程序}

catch(Exception e){//這里寫如果出現異常怎么處理的程序}

二、throws語句

  語法:函數方法() throws Exception {}

三、try語句示例:

import java.util.Scanner;public class Index{    public static void main(String[] args){        Scanner in    =    new Scanner(System.in);        try{            System.out.

四、throws語句示例:

//throws Exception 的用法import java.util.Scanner;public class Index{    public static void main(String[] args) throws Exception    //程序中的異常處理代碼都不能處理所以反饋到這里    {        try{            Myclass.t();    //調用靜態方法,測試異常,如果還有錯,在向上反饋,在往上反饋就是try語句,如果這個語句在無法處理,就反饋到主函數        }        catch(Exception e){            System.out.println("輸入錯誤");        }        System.out.println("程序繼續執行");    }}class Myclass{    static void t() throws Exception    //如果本方法有錯,向上反饋    {        Scanner in    =    new Scanner(System.in);        System.out.println("請輸入一個數字:");        int a    =    in.nextInt();        System.out.println("請輸入一個數字:");        int b    =    in.nextInt();        int c    =    a%b;        System.out.println("余數為:"+c);    }}

五、finally

//finally 的用法import java.util.Scanner;public class Index{    public static void main(String[] args) throws Exception        {        try{            Myclass.t();        }        catch(Exception e){            System.out.println("輸入錯誤");        }        finally{            System.out.println("不管是否異常,我都會繼續執行");//finally就是代表這個意思,這個是跟try一起使用的        }    }}class Myclass{    static void t() throws Exception        {        Scanner in    =    new Scanner(System.in);        System.out.println("請輸入一個數字:");        int a    =    in.nextInt();        System.out.println("請輸入一個數字:");        int b    =    in.nextInt();        int c    =    a%b;        System.out.println("余數為:"+c);    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 云浮市| 南开区| 山西省| 辛集市| 柳河县| 灯塔市| 成安县| 怀集县| 合肥市| 高安市| 西吉县| 台前县| 东宁县| 淮滨县| 郴州市| 孟州市| 武陟县| 青海省| 南涧| 墨脱县| 东莞市| 介休市| 昭苏县| 天峨县| 临邑县| 富源县| 永济市| 邵武市| 吉隆县| 来凤县| 大埔区| 博野县| 永吉县| 郓城县| 濉溪县| 怀化市| 庆元县| 信宜市| 隆子县| 锡林浩特市| 灵山县|