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来创建的额,所以不需要自己处理垃圾。

 

 

 

                              

0 0
原创粉丝点击