java throws的作用_java中throws实例用法详解

1f76b5a2b45ffb54defe6c81b8358f16.png

在程序出现异常时,会有一个抛出异常的throw出现,这里我们要跟今天所讲的throws区分开。throws的作用是声明抛出,在名称上也跟throw有所不同。下面我们就throws对策概念、语法、实例带来讲解,帮助大家找到声明抛出异常的方法,具体方法如下。

1.概念

如果方法声明的是Exception类型的异常或者是Checked Exception异常,要求方法的调用处必须做处理。

(1)继续使用throws向上(方法的调用处)声明

(2)使用try-catch-finally进行处理

2.语法

[(修饰符)](返回值类型)(方法名)([参数列表])[throws(异常类)]{......}

public void function() throws Exception{......}

3.实例

class MyException extends Exception {

public MyException() {}

public MyException(String msg) {

super(msg);

}

}

public class Demo3 {

public static void main(String[] args) {

try {

test();

} catch (MyException e) {

System.out.println("Catch My Exception");

e.printStackTrace();

}

}

public static void test() throws MyException{

try {

int i = 10/0;

System.out.println("i="+i);

} catch (ArithmeticException e) {

throw new MyException("This is MyException");

}

}

}

到此这篇关于java中throws实例用法详解的文章就介绍到这了,更多相关java中throws的使用内容请搜索云海天教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持云海天教程!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以通过FTP工具类来操作FTP,以下是一个示例: ``` import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.SocketException; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; public class FTPUtil { private static final String FTP_ADDRESS = "ftp服务器地址"; private static final int FTP_PORT = 21; private static final String FTP_USERNAME = "ftp用户名"; private static final String FTP_PASSWORD = "ftp密码"; // 连接到FTP服务器 public static FTPClient connectFTP() throws SocketException, IOException { FTPClient ftpClient = new FTPClient(); ftpClient.connect(FTP_ADDRESS, FTP_PORT); ftpClient.login(FTP_USERNAME, FTP_PASSWORD); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { ftpClient.disconnect(); throw new IOException("连接FTP服务器失败"); } return ftpClient; } // 上传文件到FTP服务器 public static void uploadFile(String remotePath, String fileName, InputStream input) throws IOException { FTPClient ftpClient = null; try { ftpClient = connectFTP(); ftpClient.changeWorkingDirectory(remotePath); // 切换到上传目录 ftpClient.storeFile(fileName, input); } finally { if (ftpClient != null) { ftpClient.logout(); ftpClient.disconnect(); } } } // 从FTP服务器下载文件 public static void downloadFile(String remotePath, String fileName, OutputStream output) throws IOException { FTPClient ftpClient = null; try { ftpClient = connectFTP(); ftpClient.changeWorkingDirectory(remotePath); // 切换到下载目录 ftpClient.retrieveFile(fileName, output); } finally { if (ftpClient != null) { ftpClient.logout(); ftpClient.disconnect(); } } } // 删除FTP服务器上的文件 public static boolean deleteFile(String remotePath, String fileName) throws IOException { FTPClient ftpClient = null; try { ftpClient = connectFTP(); ftpClient.changeWorkingDirectory(remotePath); // 切换到删除目录 return ftpClient.deleteFile(fileName); } finally { if (ftpClient != null) { ftpClient.logout(); ftpClient.disconnect(); } } } } ``` 使用方式: 上传文件: ``` File file = new File("本地文件路径"); InputStream input = new FileInputStream(file); FTPUtil.uploadFile("上传目录", "上传的文件名", input); ``` 下载文件: ``` OutputStream output = new FileOutputStream(new File("本地文件路径")); FTPUtil.downloadFile("下载目录", "下载的文件名", output); ``` 删除文件: ``` FTPUtil.deleteFile("删除目录", "删除的文件名"); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值