Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Exception Handling
shaneangel



发贴: 0
积分: 0
于 2003-10-16 09:15 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1.Core Concept:
Regarded Region:
The source code within the try clause of your programme.

Ducking Exception:
If the method provided a catch clause for a particular exception, this method is called “ducking” exception.

Exception Matching:
When an exception is thrown, Java will try to find a catch clause for the exception. If it doesn’t find one, it will search for a handler for a supertype of the exception. If it doesn’t find a catch clause that matches a superytpe for the exception, then the exception is propagated up the call stack. This process is called Exception Matching.

Catch or Specify Requirement:
Each method must either handle all checked exceptions by applying catch clause or list each unhandled checked exception as a thrown exception. This rule is referred to as Java’s catch or specify requirement.

Checked Exception:
the all subclasses of Exception are called checked Exception, in this case it’s necessary to catch this type of exception or specify it by used throws clause followed by define the class.

Unchecked Exception:
the all subclasses of Error or RuntimeException, these exception need not to be caught or specified, it just represent program bugs, like RuntimeException.

3.Formatting: (Pseudocode)
try {
//This is the first line of the “guarded region”
//that is governed by the try keyword.
//Put code here that might cause some kind of exception.
//We may have many code lines here or just one.
// This clause is necessary in handling to exception.
} catch (NamingException VariableofException) {
//Put code here that handles this error.
//This is the next line of the exception handler.
//This is the last line of the exception handler.
//This clause not required.
} finally {
//Put code here that you want to do that regardless if the exception occurred.
//Normally, used to release the system resource and to perform any needed cleanup
//for the try clause. This clause not required.
}

4.The flow control in Exception handler
As we are similar, the flow enter into the try block code directly, if have anything happened, it can go downward step by step, but the code in which catch clause isn’t be executed, then go into the finally clause to execute the code in it, last the code will be executed that following the finally clause. Once the any type of exception will be occurred, this flow control changes into the first line within catch clause that is relative with the NamingException. Immediately then the finally clause will be executed.

5.The benefits of using Exception Handler Mechanism
There are three primary benefits of exception handling:
²  Exception Handlers Are Separated From A Method’s Primary Logic.
²  Similar Errors Can Be handled Together.
²  Exception Propagation.

6.How to define the Exception belong to you
Create your own exceptions by subclassing any existing Exception class or any of its subclasses. Each exception has two constructors: non-arguments default constructor, and one that accepts a String as parameter. Of course, this class could have all methods that derived from the supertype of particular exception.

7.The useful method of the Exception

fillInStackTrace()
It is used primarily to hide the original location where the exception was created. The stack information placed in the exception object when it was created is replaced with the stack trace to the current location when its method is called.
printStackTrace()
Print the stack trace information, it will indicate the exception was thrown from the method that contained the throw keyword, not the location from which the original exception was thrown.
getMessage()
Return the string that instantiated an Exception pass into it as arguments that are String type.


shaneangel edited on 2003-10-16 09:19


话题树型展开
人气 标题 作者 字数 发贴时间
2335 Exception Handling shaneangel 3910 2003-10-16 09:15

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923