Java异常处理之throws抛出异常
package com.test;
import java.io.FileReader;
public class Test2 {
public static void main(String[] args) throws Exception
{
Father father = new Father();
father.test1();
father.test2();
}
}
class Father{
private Son son = null;
public Father()
{
this.son = new Son();
}
public void test1()
{
System.out.println("1");
//要调son的test2必须要捕获或者抛出一层层往上抛,最终抛给虚拟机
try {
son.test2();
} catch (Exception e) {
// TODO: handle exception
System.out.println("把一个异常交给上一层调用者去处理");
System.out.println("父亲在处理");
e.printStackTrace();
}
}
//或者都不管一层层向上 throws,一层层向上抛,跑到main函数最终抛给JVM虚拟机
public void test2() throws Exception
{
System.out.println("1");
//要调son的test2必须要捕获或者抛出一层层往上抛,最终抛给虚拟机
son.test2();
}
}
class Son{
public void test2() throws Exception
{
FileReader fr = null;
fr = new FileReader("D:\\aa.txt");
}
}
Java异常处理之throws抛出异常的更多相关文章
- Java基础学习总结(86)——Java异常处理机制Exception抛出异常时throw和throws用法详解
什么时运行时异常?什么是非运行时异常? 通俗的讲: 运行时异常:就是编译通过,运行时就崩了,比如数组越界. 非运行时异常:就是编译不通过,这时就得必须去处理了.不然就没法运行了. 全面的讲: Thro ...
- 75.Java异常处理机制-手动抛出异常
package testDate; import java.io.File; import java.io.FileNotFoundException; public class TestReadFi ...
- 75.Java异常处理机制throws
package testDate; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IO ...
- java异常处理机制throws
throws可以在方法声明时抛出的异常.原则上throws声明的异常,一定要在该方法中抛出,否则没有意义. 相反的,若方法中我们主动通过throw抛出一个异常,应该在throws中声明该种类异常,通知 ...
- Java 异常处理 try catch finally throws throw 的使用和解读(一)
//最近的一个内部表决系统开发过程中,//发现对异常处理还存在一些模棱两可的地方,//所以想着整理一下//主要涉及到://1.try catch finally throws throw 的使用和解读 ...
- java异常处理之throw, throws,try和catch
转自 http://blog.csdn.net/zhouyong80/article/details/1907799 程序运行过程中可能会出现异常情况,比如被0除.对负数计算平方根等,还有可能会出现 ...
- Java中的异常处理:何时抛出异常,何时捕获异常,何时处理异常?
Java中的异常处理:何时抛出异常,何时捕获异常? 2017-06-07 1 异常分类 Throwable对象可以分为两组: 一组是unchecked异常,异常处理机制往往不用于这组异常,包括: Er ...
- 札记:Java异常处理
异常概述 程序在运行中总会面临一些"意外"情况,良好的代码需要对它们进行预防和处理.大致来说,这些意外情况分三类: 交互输入 用户以非预期的方式使用程序,比如非法输入,不正当的操作 ...
- java异常处理(父子异常的处理)
我当初学java异常处理的时候,对于父子异常的处理,我记得几句话“子类方法只能抛出父类方法所抛出的异常或者是其子异常,子类构造器必须要抛出父类构造器的异常或者其父异常”.那个时候还不知道子类方法为什么 ...
随机推荐
- scala学习笔记:match与unapply()
编写如下代码: object MatchTest { def foo(a : Any) : String = { a match { case 1 => "int:1" ca ...
- SQL SERVER 级联删除
有三个表: Company Address Contact 在Address和Contact中建立外键,外键id为company的id, 那么就不能任意删除Company.但假如在外键约束中把级联删除 ...
- dubbo监控活跃线程数
telnet对应dubbo服务的ip+端口号 status -l 其中的active就是当前的活跃线程数 通过程序定时探测写入DB,再查询渲染出来就好了 监控报警,如果已经有监控平台,可以通过一定的规 ...
- 写入.csv文件
#include "stdafx.h" #include "WriteCsv.h" CString m_strData;//写入记录的一条数据 CString ...
- [leetcode] 403. Frog Jump
https://leetcode.com/contest/5/problems/frog-jump/ 这个题目,还是有套路的,之前做过一道题,好像是贪心性质,就是每次可以跳多远,最后问能不能跳到最右边 ...
- JSON parser error with double quotes
Use backslash charater \ to escape double quotes in JSON file as below example. { "myInfo" ...
- Xml通用操作类
using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Xml ...
- Nosql_笔记
Nosql: http://www.infoq.com/cn/news/2011/01/nosql-why/ Redis: http://www.jb51.net/article/59294.htm ...
- PHP LINUX Notice: undefined $_GET完美解决方法
PHP Notice: undefined 平时用$_GET[‘xx’] 取得参数值时,如果之前不加判断在未传进参数时会出现这样的警告: PHP Notice: undefined index xxx ...
- VC++对话框中添加状态栏的方法
方法一:1.添加成员变量CStatusBarCtrl m_StatusBar;2.在OnInitDialog()中加入: m_StatusBar.Create(WS_ ...