zoukankan      html  css  js  c++  java
  • java 异常处理

    1.使用throws关键字放在方法名括号的外面,修饰改方法,表示 抛出的异常给调用该方法的方法处理,也就是说谁调用谁负责。

    2.使用throw关键字放在try{}catch(exection e){}的try里面,表示抛出的异常由本方法自己处理。若没有放在try{}块中,则需要加上throws关键字抛出该异常由调用这个方法的方法处理。

    测试程序:

     1 package com;
    2
    3 public class ExTest {
    4
    5 public static void main(String[] args){
    6 ExTest t = new ExTest();
    7 int flag = t.testThr();
    8 System.out.println("main= "+flag);
    9 }
    10 public int testThr(){
    11 int b = 0;
    12 try {
    13 b = threxc();
    14 } catch (Exception e) {
    15 System.out.println("testThr: "+e.getMessage());
    16 e.printStackTrace();
    17 }
    18 return b;
    19 }
    20 public int threxc() throws Exception
    21 {
    22 try{
    23 int a = 4/0;
    24 return a;
    25 }catch (Exception e) {
    26 System.out.println("threxc: "+e.getMessage());
    27 throw e;
    28
    29 }
    30 //return 1;
    31 }
    32 }



  • 相关阅读:
    freak out用法
    kinda用法
    比较级与最高级
    issue用法
    invite用法
    yet用法
    follow用法
    get用法
    turn up&turn off&turn on用法
    关于document.lastModified属性
  • 原文地址:https://www.cnblogs.com/pandaXiong/p/2434146.html
Copyright © 2011-2022 走看看