java异常
来源:互联网 发布:国家大力发展人工智能 编辑:程序博客网 时间:2024/06/10 01:12
1.java异常是用来做什么的
java使用异常来提供一致的错误报告模型
2.创建异常类
//所有自定义的异常类都必须继承已存在的异常类public class Myexception extends Exception{ public Myexception() { } public Myexception(String message) { super(message); }}
public class exceptionTest { public void f() throws Myexception { throw new Myexception(); } public void g() throws Myexception { throw new Myexception("这是g异常"); } @Test public void test1() { try { FileOutputStream fileOutputStream = new FileOutputStream("H:\\javaio\\error.txt"); PrintStream printStream = new PrintStream(fileOutputStream);//printWriter无法将信息打印到文件中?? FileWriter fileWriter = new FileWriter("H:\\javaio\\error2.txt"); PrintWriter printWriter = new PrintWriter(fileWriter); try { f(); } catch (Myexception e) { e.printStackTrace(printStream); } try { g(); } catch (Myexception e) { e.printStackTrace(printWriter); } } catch (IOException e) { e.printStackTrace(); } }}
3.异常类的printStackTrace()方法
//此方法有四个重载 public void printStackTrace() { printStackTrace(System.err); } public void printStackTrace(PrintStream s) { printStackTrace(new WrappedPrintStream(s)); } public void printStackTrace(PrintWriter s) { printStackTrace(new WrappedPrintWriter(s)); }//默认通过标准出错流打印到控制台,也可以通过标准输出System.out打印到控制台//可以传入PrintStream对象或者其子类对象 PrintStream-->FilterOutputStream-->OutputStream//可以传入PrintWriter对象或其子类对象 PrintWriter-->Writer
4.异常链
通常需要在处理一个异常后在抛出另一个异常,需要在catch块中再执行throw语句抛出新的异常
阅读全文
0 0
- java 异常,java,异常
- Java 异常 异常链
- Java异常
- Java 异常
- JAVA 异常
- Java异常
- java 异常
- Java异常
- java异常
- Java异常
- java异常
- java异常
- java异常
- java 异常
- java异常
- java异常
- java异常
- Java异常
- 设计模式--原型模式
- 《重构——改善既有代码的设计》【PDF】下载
- Python爬虫学习纪要(十二):BeautifulSoup相关知识点4
- 设计模式--原型模式
- Oracle 建立索引及SQL优化
- java异常
- 12月19日 数据结构 周二
- 关于段错误的知识总结
- java的equals字符串返回false
- 四分树
- Meizu Log.v Log.d 打印不出来
- Python实现IOC控制反转
- 深度网络的过拟合问题讨论
- Mybatis_resultMap_级联、分布、延迟加载