异常处理机制:两种方式

news/2024/7/4 10:09:36

throws + 异常类型

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author xianyu
 * @version 1.0
 * @date 2020/1/1 15:53
 */
public class ExceptionTest2 {

    public static void main(String[] args){

        // 可以在最终调用时处理
        try {
            method2();
        } catch (IOException e) {
            e.printStackTrace();
        }

        method3();
    }


    // method3进行了异常处理
    public static void method3(){
        try {
            method2();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 这里任然向上抛出异常
    public static void method2() throws IOException{
        method1();
    }

    // 异常跑出去,给调用该方法的方法去处理
    public static void method1() throws FileNotFoundException,IOException {
        File file = new File("hello1.txt");
        FileInputStream fis = new FileInputStream(file);

        int data = fis.read();
        while(data != -1){
            System.out.print((char)data);
            data = fis.read();
        }

        fis.close();


    }



}



try – catch – finally

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * @author xianyu
 * @version 1.0
 * @date 2020/1/1 15:35
 */
public class ExcceptionTest {

    public static void main(String[] args) {
        // 异常测试
        String str = "123";
        //str = "abc";
        int num = 0;
        try{
            num = Integer.parseInt(str);
        }catch (NumberFormatException e){
            //System.out.println("出现数值转换异常了");
            e.printStackTrace();
        }
        System.out.println(num);
        System.out.println("***************************");

        File file = new File("hello.txt");
        try {
            FileInputStream fis = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


    }
}


http://www.niftyadmin.cn/n/1791014.html

相关文章

MapReduce之Reduce Join

一 介绍 Reduce Join其主要思想如下: 在map阶段,map函数同时读取两个文件File1和File2,为了区分两种来源的key/value数据对,对每条数据打一个标签(tag), 比如:tag0表示来自文件File1…

Java的23种设计模式之单列设计模式

饿汉式单列模式类:在这个类每次加载到内存中就直接把instance实例化好了 package com.xianyu.singleton;/*** author xianyu* version 1.0* date 2020/3/12 16:35* 设计模式1: 单列设计模式*/ public class Program1 {public static void main(String[] args) {// …

vue-cli keep-alive用法以及activated,deactivated

keep-alive用法 <keep-alive>是Vue的内置组件&#xff0c;能在组件切换过程中将状态保留在内存中&#xff0c;防止重复渲染DOM。 include: 字符串或正则表达式。只有匹配的组件会被缓存。exclude: 字符串或正则表达式。任何匹配的组件都不会被缓存。import Vue from vue …

小型电子声光礼花器电子烟花爆竹电路设计

节日和庆典时燃放礼花&#xff0c;其绚丽缤纷的图案&#xff0c;热烈的爆炸声、欢乐的气氛&#xff0c;能给人们留下美好的印象&#xff0c;但有一定的烟尘污染和爆炸危险隐患。本电路可以模拟礼花燃放装置&#xff0c;达到声型兼备的效果&#xff0c;给人们在安全、环保的环境…

spring boot 配置 JPA

application.properties的配置 spring.datasource.platformpostgres spring.datasource.urljdbc:postgresql://localhost:5432/to_database spring.datasource.usernamepostgres spring.datasource.password123456789 spring.jpa.properties.hibernate.dialect org.hibernate.…

字节顺序

字节顺序 #include <stdio.h>typedef int* int_ptr; typedef unsigned char* byte_ptr;void show_bytes( byte_ptr start, int len ){int i;for( i 0; i < len; i ){printf( " %4.2x", start[i] );}printf( "\n" );}void show_int( int x ){show…

Java原型模式--拷贝

package com.xianyu.prototype;import java.util.Date;/*** author xianyu* version 1.0* date 2020/3/15 21:36* 复制视频源文件* 1. 实现一个接口* 2. 重写一个方法*/ public class Video implements Cloneable{ // 实现一个接口private String name;private Date createTim…

GPA低于2.0被开除

GPA低于2.0被开除&#xff08; Q 575121832&#xff09; GPA太低会被开除吗&#xff1f; GPA是衡量学生学术水平的一个重要指标。一般而言&#xff0c;如果连续两学期&#xff0c;本科生的GPA达不到2.0、研究生的GPA达不到3.0&#xff0c;都有可能面临被学校劝退或开除的情况。…