{"rsdb":{"rid":"158033","subhead":"","postdate":"0","aid":"118488","fid":"54","uid":"1","topic":"1","content":"

\u5728Java\u4e2d\uff0c\u5f02\u5e38\u5206\u4e3a\u7f16\u8bd1\u65f6\u5f02\u5e38\u548c\u8fd0\u884c\u65f6\u5f02\u5e38\u3002\u7f16\u8bd1\u65f6\u5f02\u5e38\u53c8\u53eb\u7f16\u8bd1\u65f6\u88ab\u76d1\u6d4b\u7684\u5f02\u5e38\uff1a\u5728\u7a0b\u5e8f\u7f16\u8bd1\u8fc7\u7a0b\u4e2d\u76d1\u6d4b\u5230\u975e\u8fd0\u884c\u65f6\u5f02\u5e38\u7684\u5f02\u5e38\uff0c\u51fa\u73b0\u8be5\u5f02\u5e38\u8981\u4e48\u5411\u4e0a\u629b\u51fa\uff0c\u8981\u4e48\u6355\u83b7\u5904\u7406\u3002<\/p>

\u8fd0\u884c\u65f6\u5f02\u5e38\uff08runtimeException\u53ca\u5176\u5b50\u7c7b\uff09:\u5728\u7a0b\u5e8f\u7f16\u8bd1\u8fc7\u7a0b\u4e2d\u76d1\u6d4b\u5230\u51fa\u73b0\u8fd0\u884c\u65f6\u5f02\u5e38\uff0c\u51fd\u6570\u4e0a\u6216\u5185\u90e8\u53ef\u4ee5\u76f4\u63a5\u629b\u51fa\u5f02\u5e38\uff0c\u4e0d\u9700\u8981\u6355\u83b7\u548c\u58f0\u660e\u3002<\/p>

\u7f16\u8bd1\u65f6\u5f02\u5e38\uff1a
\u3000\u3000\u5728\u67d0\u4e2a\u65b9\u6cd5\u4e2d\uff0ctry\u4e2d\u51fa\u73b0\u5f02\u5e38\uff0c\u4f1a\u5c06\u5f02\u5e38\u4fe1\u606f\u5c01\u88c5\u6210\u4e00\u4e2a\u5bf9\u5e94\u5f02\u5e38\u4fe1\u606f\u7684\u5f02\u5e38\u5bf9\u8c61\uff08new Exception\uff09\uff0c\u7136\u540e\u5728catch\u4e2d\u5c06\u8fd9\u4e2a\u5f02\u5e38\u5bf9\u8c61\u8fdb\u884c\u6355\u83b7\uff0c\u7136\u540e\u8fdb\u884c\u5f02\u5e38\u7684\u5904\u7406\u3002<\/p>

\u3000\u3000\u5728main\u65b9\u6cd5\u4e2d\uff0c\u5982\u679c\u51fa\u73b0\u5f02\u5e38\uff0c\u5982\u679c\u6ca1\u6709\u8fdb\u884c\u5f02\u5e38\u5904\u7406\uff0c\u6216\u8005\u5728\u5f02\u5e38\u5904\u7406\u4e2d\u53c8\u51fa\u73b0\u5f02\u5e38\u7684\u629b\u51fa\uff0c\u5f53\u5f02\u5e38\u4e00\u5c42\u5c42\u7684\u5f80\u4e0a\u629b\uff0c\u5374\u6ca1\u6709\u5f97\u5230\u89e3\u51b3\u65f6\uff0c\u6700\u7ec8\u4f1a\u629b\u7ed9 JVM\uff0cJVM\u4f1a\u5c06\u5f02\u5e38\u6267\u884c\u9ed8\u8ba4\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u3002<\/p>

public static void main(String[] args)throws Exception{
        try{
            throw new Exception("1");
        }catch(IOException e){
            throw new Exception("2");
        }catch(Exception e){
            throw new Exception("3");
        }finally{
            throw new Exception("4");
        }
    }<\/p>

\u3000\u3000\u8fd9\u6bb5\u4ee3\u7801\u7684\u6700\u7ec8\u7ed3\u679c\u662f\uff1a4\u3002<\/p>

\u3000\u3000\u56e0\u4e3afinally\u6700\u7ec8\u4f1a\u88ab\u6267\u884c\uff0c\u5982\u679c\u5c06finally\u53bb\u6389\uff0c\u90a3\u4e48\u4f1a\u6253\u5370\u51fa3\uff0c\u56e0\u4e3a3\u6700\u7ec8\u662f\u629b\u7ed9JVM\u4e86\uff0cJVM\u4f1a\u4f7f\u7528\u9ed8\u8ba4\u7684\u65b9\u5f0f\u8fdb\u884c\u6253\u5370\u8f93\u51fa\u3002<\/p>

\u3000\u3000\u5728\u4e0a\u6bb5main\u65b9\u6cd5\u4e2d\uff0c\u51fa\u73b0throws\u548cthrow\uff0c\u4e24\u8005\u7684\u533a\u522b\u662fthrows\u4f7f\u7528\u5728\u51fd\u6570\u4e0a\uff0c\u540e\u9762\u53ef\u8ddf\u591a\u4e2a\u5f02\u5e38\u3002throw\u4f7f\u7528\u51fd\u6570\u5185\uff0c\u540e\u9762\u8ddf\u4e00\u4e2a\u5f02\u5e38\u5bf9\u8c61\uff08new Exception\uff09<\/p>

\u3000\u3000\u4e24\u8005\u7684\u76f8\u540c\u70b9\u662f\u90fd\u9700\u8981\u8fdb\u884c\u5f02\u5e38\u7684\u6355\u83b7\u5904\u7406\u3002<\/p>

\u3000\u3000\u591a\u5f02\u5e38\u7684\u5904\u7406\uff08\u7f16\u8bd1\u65f6\u5f02\u5e38\uff09\uff1a<\/p>

\u3000\u3000\u3000\u30001\u3001\u58f0\u660e\u5f02\u5e38\u65f6\uff0c\u5efa\u8bae\u58f0\u660e\u66f4\u4e3a\u5177\u4f53\u7684\u5f02\u5e38\uff0c\u8fd9\u6837\u5904\u7406\u4e5f\u4f1a\u66f4\u5177\u4f53.
\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u591a\u4e2a\u5f02\u5e38\u5904\u7406\uff0c\u591a\u4e2acatch\u6355\u83b7
\u3000\u3000\u3000\u30002\u3001\u5bf9\u65b9\u58f0\u660e\u591a\u4e2a\u5f02\u5e38\uff0c\u5c31\u5bf9\u5e94\u6709\u591a\u4e2acatch\u5feb
\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u5982\u679c\u591a\u4e2acatch\u5feb\u4e2d\u7684\u5f02\u5e38\u51fa\u73b0\u7ee7\u627f\u5173\u7cfb\uff0c\u7236\u7c7bcatch\uff08Exception\uff09\u5feb\u653e\u6700\u4e0b\u9762\u3002
\u3000\u3000\u3000\u30003\u3001\u591a\u5f02\u5e38\u5904\u7406\u4e2d\uff0c\u7a0b\u5e8f\u4e00\u6b21\u53ea\u5904\u7406\u4e00\u4e2a\u5f02\u5e38\u3002
\u3000\u3000\u3000\u3000\u3000\u5728\u591a\u5f02\u5e38\u7684\u5904\u7406\u4e2d\uff0c\u5982\u679c\u5728\u4e00\u4e2a\u65b9\u6cd5\u4e2d\u51fa\u73b0\u4e86\u5f02\u5e38\uff0c\u90a3\u4e48\u7a0b\u5e8f\u4f1a\u5728\u7f16\u8bd1\u65f6\u76d1\u6d4b\u5230\u7b2c\u4e00\u4e2a\u5f02\u5e38\u65f6\uff0c\u5c31\u4f1a\u89e6\u53d1\u5f02\u5e38\u7684\u5904\u7406\u673a\u5236\uff0c\u53bb\u7a0b\u5e8f\u4e2d\u67e5\u5230\u5bf9\u5e94\u8be5\u5f02\u5e38\u7684\u5f02\u5e38\u6355\u83b7\u5feb\uff08catch\uff09\u3002<\/p>

\u6ce8\u610f\uff1a\u5f53\u67d0\u4e2a\u8bed\u53e5\u51fa\u73b0\u5f02\u5e38\uff0c\u8be5\u8bed\u53e5\u8c03\u7528\u7684\u65b9\u6cd5\u5e94\u8be5\u58f0\u660e\u5f02\u5e38\u629b\u51fa\uff0c\u4e4b\u540e\u8be5\u8bed\u53e5\u5e94\u8be5\u6355\u83b7\u5f02\u5e38\uff08try-catch\uff09\uff0c\u6216\u8005\u5c06\u8be5\u8bed\u53e5\u6240\u5728\u7684\u65b9\u6cd5\u4e0a\u629b\u51fa\u5f02\u5e38\uff08throws\uff09\u3002\u629b\u7ed9JVM\u53bb\u5904\u7406\u3002<\/p>

\u8fd0\u884c\u65f6\u5f02\u5e38\uff08RuntimeException\uff09\uff1a<\/p>

\u3000\u3000Exception\u4e2d\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u5b50\u7c7b\u5f02\u5e38RuntimeException\u8fd0\u884c\u65f6\u5f02\u5e38\u3002<\/p>

\u3000\u30001\u3001\u5982\u679c\u5728\u51fd\u6570\u5185\u5bb9\u629b\u51fa\u5f02\u5e38\uff0c\u51fd\u6570\u4e0a\u53ef\u4ee5\u4e0d\u7528\u58f0\u660e\uff0c\u7f16\u8bd1\u4e00\u6837\u901a\u8fc7\u3002<\/p>

\u3000\u30002\u3001\u5982\u679c\u5728\u51fd\u6570\u4e0a\u58f0\u660e\u4e86\u8be5\u5f02\u5e38\uff0c\u8c03\u7528\u8005\u53ef\u4ee5\u4e0d\u7528\u8fdb\u884c\u5904\u7406\uff0c\u7f16\u8bd1\u4e00\u6837\u4e5f\u901a\u8fc7\u3002<\/p>

\u3000\u3000\u3000\u3000\u3000\u3000\u4e4b\u6240\u4ee5\u4e0d\u7528\u5728\u51fd\u6570\u4e0a\u58f0\u660e\uff0c\u662f\u56e0\u4e3a\u4e0d\u9700\u8981\u8ba9\u8c03\u7528\u8005\u53bb\u5904\u7406\u3002<\/p>

\u3000\u30003\u3001\u5f53\u8be5\u5f02\u5e38\u53d1\u751f\uff0c\u5176\u5b9e\u662f\u5e0c\u671b\u7a0b\u5e8f\u505c\u6b62\uff0c\u56e0\u4e3a\u5728\u8fd0\u884c\u65f6\uff0c\u51fa\u73b0\u4e86\u65e0\u6cd5\u7ee7\u7eed\u8fd0\u7b97\u7684\u60c5\u51b5\uff0c\u5e0c\u671b\u505c\u6b62\u7a0b\u5e8f\u540e\uff0c\u7a0b\u5e8f\u5458\u4f1a\u4ee3\u7801\u8fdb\u884c\u4fee\u6b63\u3002<\/p>

public class ExceptionDemo {
   
    private static double div(int i, int j) {
        if(j <= 0){
            throw new ArithmeticException();
        }
        double resoult = i\/j;
        return resoult;
    }
   
    public static void main(String[] args){
        int i = 4,j=0;
        double x = div(i,j);
        System.out.println("x= "+x);
    }<\/p>

\u3000\u3000\u5728\u4e0a\u6bb5\u4ee3\u7801\u4e2d\u58f0\u660e\u4e86ArithmeticException\u5f02\u5e38\uff0c\u8be5\u5f02\u5e38\u65f6RunnableException\u5f02\u5e38\u7684\u5b50\u7c7b\u3002<\/p>

\u3000\u3000\u4e5f\u53ef\u4ee5\u5c06ArithmeticException\u5f02\u5e38\u6362\u6210\u6211\u4eec\u81ea\u5df1\u5b9a\u4e49\u7684\u5f02\u5e38(OtherException )\u3002\u5f53\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38\u65f6\uff0c\u5982\u679c\u8be5\u5f02\u5e38\u7684\u53d1\u751f\uff0c\u65e0\u6cd5\u7ee7\u7eed\u8fdb\u884c\u8fd0\u7b97\uff0c\u5c31\u8ba9\u81ea\u5b9a\u4e49\u5f02\u5e38\u7ee7\u627fRunnableException<\/p>

public class OtherException extends RuntimeException{
   
    public OtherException() {
        System.out.println("\u81ea\u5b9a\u4e49\u5f02\u5e38\u6267\u884c\u4e86");
    }
 
}<\/p>","orderid":"0","title":"Java\u57fa\u7840\u6559\u7a0b\u4e4b\u5f02\u5e38\u5904\u7406\u673a\u5236","smalltitle":"","mid":"0","fname":"linux\u7f16\u7a0b\u57fa\u7840","special_id":"0","bak_id":"0","info":"0","hits":"3922","pages":"1","comments":"0","posttime":"2017-07-14 10:23:04","list":"1499998984","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"Java<\/A> \u57fa\u7840<\/A> \u6559\u7a0b<\/A> \u5f02\u5e38<\/A> \u5904\u7406<\/A> \u673a\u5236<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"218.17.197.194","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"Java\u57fa\u7840\u6559\u7a0b\u4e4b\u5f02\u5e38\u5904\u7406\u673a\u5236","lastview":"1699551403","digg_num":"8211","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}