在处理抛出异常的方法时,我们可以采用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
分享到:
相关推荐
在Java中,异常处理是通过一套结构化的机制来实现的,主要包括try、catch、finally、throw和throws关键字。下面将详细探讨这些概念以及如何通过Example代码来理解它们。 1. try块: try块用于包含可能会抛出异常的...
Java提供了一套强大的异常处理机制,主要包括`try`、`catch`、`throw`、`throws`和`finally`关键字。 - **try**:用来包裹可能抛出异常的代码块。 - **catch**:捕获并处理由`try`块抛出的异常。 - **throw**:手动...
这些异常在程序设计阶段就应该考虑到,并且可以通过在方法声明中使用`throws`关键字来表明可能会抛出的异常,或者通过使用`try-catch`块来捕获和处理这些异常。例如,当尝试打开一个不存在的文件时,会抛出...
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { // 从 token 获取用户名,然后从数据库查询用户信息 // ... } @Override protected ...
throws Exception { //CatalinaProperties解析$CATALINA_HOME\conf\catalina.properties, //并将catalina.properties内的属性存为系统属性 //catalina.properties内common.loader="${catalina.base}/lib", ...
问题 10:请描述一下 Java 中的异常处理机制,包括 try-catch-finally、throw 和 throws 关键字的作用。 答案:Java 的异常处理机制通过 try-catch-finally 块来捕获和处理异常。try 块中放置可能抛出异常的代码,...
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request....
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 ...
可以使用Java的try-catch语句来捕获并处理异常。例如: ```java public static Object execute(Command cmd) { try { Connection conn = getConnection(cmd); Object result = cmd.execute(conn); conn.close()...
The JVM throws this exception when you try to call a method or access a field on a null object reference. To handle it, you should always check if the reference is null before attempting to use it. ...
* 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...
- 异常捕捉:使用try...catch...finally语句块来捕获并处理异常。finally块确保即使在try块中发生异常,某些代码(如资源清理)仍会执行。 - 异常抛出:使用throws关键字在方法声明中抛出异常,将异常处理交给上层...
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 ...
解析:Java异常处理机制主要包括`try`、`catch`、`finally`和`throws`等关键字。 - **A**: 错误,对于编译期检查的异常,必须通过`try-catch`块捕捉或通过`throws`声明抛出;而对于运行时异常如`RuntimeException`...
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"/> </...