JAVA学习 之 异常处理

C语言的异常处理都通过函数的返回值类型判断给予处理,而在java中,异常被定义为一个对象,有这一套完善的异常处理机制。

一、异常的捕获

二、手动抛出异常

package zjl;

import java.util.InputMismatchException;
import java.util.Scanner;

import javax.sound.midi.SysexMessage;

import org.omg.CORBA.SystemException;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

public class zuoye {
    public static int sum(int n)
    {
        if(n > 100)
            throw new RuntimeException();
            int s = 0;
            int i;
            for(i = 1;i <= n;i ++){
                s += i;
            }
            return s;
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
         Scanner text = new Scanner(System.in);
         int a;
         try{
             a = text.nextInt();
             System.out.println(sum(a));
         }
         catch(RuntimeException e){
             System.out.println("你给的数有问题");
         }
         finally{
             System.out.println("我总是出现");
         }
    }

}

 

posted on 2016-03-17 10:59  Tob's_the_top  阅读(159)  评论(0编辑  收藏  举报

导航