java学习笔记--Exception
来源:互联网 发布:数控切割机编程代码 编辑:程序博客网 时间:2024/05/01 03:49
1、 in all standard Exception,there are two constructors: the default one and the other with a string argument which can be used to show specific problems information.
2、 throw new NullPointerException(). After creating an exception object with new, you give the result reference to throw. The object is , in effect ,“returned ” from the method.
You can throw type of Throwable. Often , the only information is the type of exception, and nothing meaningful stored in the exception object.
3、 if you throw an exception in a method, that method will exit in the processing of throwing.
you can use try catch if you don’t want to exit; catch is the exception handler. The search for handler stops once the catch clause in finished, only the match catch executes,it’s not like a switch statement in which you need a break after each case to prevent the remaining ones from executing.
4、 Termination VS resumption, java supports termination, if you want resumption-like behavior in java , don’t throw an exception when encounter an error. Instead, place your try block inside a while loop.
5、 e.printStackTrace(): show the exception information, the innermost willbe shown in the first;
and the outermost in the last(usually is our program).Default print to the System.err, you can add System.out to let it to System.out.
6、 Exceptions and logging: p452 没弄懂。
7、 The exception specification: 当我们throw 一个exception时,我们需要在该方法的声明处用throws 加Exceptions(多个用逗号隔开)来声明该方法所要抛出的异常(没有在方法内catch时)。throws 后的Exception可多不可少。
8、 Exception 类放在catch的最后可以接收我们用到的所有的Exception。
可以对接收到的Exception对象用如下的方法:
String getMssage()à 创建Exception时的那个String参数
String getLocalizedMessage()à get the detail message, or a message adjusted for this particular locale.????????????????????????????
String toString()à包名.类名:String参数
void PrintStackTrace()à toString()+方法引用栈从里到外,standard error。(注意这里已经包含print的功能,不需要早套一个print)
void printStackTrace(PrintStream)à 指定PrintStream
void printStackTrace(java.io.PrintWriter)à指定PrintWriter
Throwable fillInStackTrace()à记录当前异常,用来二次抛出异常
还可以用Object的方法,下面是有用的一些:
getClass()à return an object representing the class of this object. You can inreturn query this Class object for its name with。实例:Class<?extends Exception> c=e.getClass();
getName()à package information;
getSimpleName()à the class name alone.
还可以:
StackTraceElement[]getStackTrace() (进而equals, getClassName, getFileName, getLineNumber, getMethodName()),在数组中最里层的那个方法下标为0,?main是数组中最后一个元素?。
9、 Rethrowing an exception:可以throw e; or throw new ThisOrOtherExcepiton()
Rethrow后,需要更高层次的Exceptionhandler来处理,这个try对应的其他所有的catch依然失效。throw e;的话,printStackTrace()时会追到最原始的那个throw所在的方法。throw new ThisOrOtherException()的话,原来e的Stack会丢失,printStackTrace()从当前点开始(包括throw e.fillStackTrace(). The line where fillStackTrace() is called becomes thenew point of origin of the exception).因为exception都是用new来创建的额,所以不需要自己处理垃圾。
- java学习笔记-EXCEPTION
- java学习笔记--Exception
- Java学习笔记之Exception
- Java 学习笔记 (4) -异常 Exception
- Java学习笔记26 异常Exception
- boost exception 学习笔记
- php学习笔记--Exception
- Java异常(Exception)小例子&知识点【Java学习笔记】
- Java菜鸟学习笔记--Exception篇(一):异常简介
- Java学习笔记:Exception:Operation not allowed after ResultSet closed
- Java Exception学习
- JAVA学习--异常Exception
- Java学习-异常(Exception)
- java 自定义Exception学习
- Java 复习笔记 异常Exception
- 【Java学习笔记】6:Java中的包(package)与Exception类异常处理
- java个人学习笔记14(Error+Exception+try_catch_finally+throw+throws)
- JAVA学习笔记25——异常机制1:Exception简介+异常处理的try_catch_finally方法
- C++虚函数(2) 虚函数剖析之虚函数表
- 【暴力搜索】[POJ 1011]Sticks
- lqkzsfhlafreultt
- 还原数据库出错
- php 判断字符串是否包含
- java学习笔记--Exception
- Android学习笔记【一】Android学习感言(包含学习见解、学习方法)
- Android笔记之四种方式实现计时器
- 表单元素input、button、submit、submit()、onsubmit()详解
- 统计难题
- 快捷键大全
- jquery ajax备用
- vtlmkyzyslaxwdqt
- Android 无障碍服务一 让应用具有辅助性服务