在处理抛出异常的方法时,我们可以采用try/catch进行捕捉 或者使用throws抛出, 但具体怎么使用, 什么时候进行try/catch,什么时候进行throws? 现通过代码简要总结.
实验1 :使用try/catch
在main方法中对methord1()抛出的异常进行捕捉
import java.io.File;
import java.io.IOException;
public class ExceptionTest {
public static void main(String[] args) {
try {
methord1();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
methord2();
}
private static void methord1() throws IOException {
// TODO Auto-generated method stub
System.out.println("methord1 ");
File f = new File("");
f.createNewFile();
System.out.println("methord1111 ");
}
private static void methord2() {
// TODO Auto-generated method stub
System.out.println("methord2 ");
}
}
,结果为:
methord1
java.io.IOException: 系统找不到指定的路径。
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:850)
at com.bryant.test.ExceptionTest.methord1(ExceptionTest.java:21)
at com.bryant.test.ExceptionTest.main(ExceptionTest.java:10)
methord2
methord2 方法也执行.
实验2.使用throws
修改main方法为throws methord1()抛出的异常:
public static void main(String[] args) throws IOException {
methord1();
methord2();
}
结果为:
methord1
Exception in thread "main" java.io.IOException: 系统找不到指定的路径。
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:850)
at com.bryant.test.ExceptionTest.methord1(ExceptionTest.java:17)
at com.bryant.test.ExceptionTest.main(ExceptionTest.java:10)
方法methord2 没有执行.
由此总结:
当出现的异常使程序后面无法执行时,抛出异常,结束程序.使用throws
当程序出现异常,不影响后续操作时,捕捉异常,后续程序将继续执行.使用try/catch
分享到:
相关推荐
throws Exception { //CatalinaProperties解析$CATALINA_HOME\conf\catalina.properties, //并将catalina.properties内的属性存为系统属性 //catalina.properties内common.loader="${catalina.base}/lib", ...
public ReveiceThread(SocketChannel channel) throws Exception { this.r_channel = channel; this.r_channel.configureBlocking(false); } public void run() { try { Selector selector = Selector.open...
* It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied. * You may study, use, and modify it for any non-commercial purpose. * You may distribute it non-commercially as long as you...
// NOTE : new ActiveXObject(strFoo) throws an exception if strFoo isn't in the registry try { // version will be set for 7.X or greater players axo = new ActiveXObject("ShockwaveFlash.Shockwave...
* <code>UPDATE</code>, or <code>DELETE</code> statement or an SQL * statement that returns nothing, such as an SQL DDL statement. 执行给定的 SQL * 语句, 这些语句可能是 INSERT, UPDATE 或者 DELETE ...
* It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied. * You may study, use, and modify it for any non-commercial purpose. * You may distribute it non-commercially as long as you...
new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE, this); // Set up a hover timer, so that a node will be automatically expanded // or collapsed // if the user lingers on it for more than a ...
DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <typeAliases> <typeAlias alias="User" type="com.yihaomen.mybatis.model.User"/> </...
public void doIt() throws Exception { Service srv; OutboundMessage msg; OutboundNotification outboundNotification = new OutboundNotification(); System.out.println("Example: Send message from a ...
(2) 类名是由程序员自己定义的 Java 标识符,每个类说明必须有 class 和类名。 (3) 类说明修饰符包括: abstract 说明一个类为抽象类,抽象类是指不能直接实例化对象的类。 final 说明一个类为最终类,即...
* Do not remove or rename the field mFd: it is used by native method close(); */ private FileDescriptor mFd; private FileInputStream mFileInputStream; private FileOutputStream mFileOutputStream...
Another useful rule of thumb: it's typically not cost effective to inline functions with loops or switch statements (unless, in the common case, the loop or switch statement is never executed)....
throw (关键字) throws (关键字) [θrәu] 抛出(异常) transient (关键字) 瞬变;临时的['trænziәnt]'(可序列化) valid 正确的,有效的 ['vælid] variable n.变量 a.可变的['vєәriәbl] volatile (关键字) 不...
//transfer location change Single User or Multi User MultiUser mu=new MultiUser(server.accept()); mu.start(); } } } 我的类直接从Thread类继承了下来.并且通过构造函数传递引用和客户...