gpt4 book ai didi

java - 为什么不能声明抛出异常类?

转载 作者:行者123 更新时间:2023-12-02 00:43:01 25 4
gpt4 key购买 nike

package com.csl.bps.util;

import java.awt.Event;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.HashMap;

import javax.servlet.ServletContextEvent;

public class Reloader implements Runnable{
private boolean firstLoad = true;
private ServletContextEvent eventCopy = null;
private File configFile = null;
private String configFilePath = null;
private HashMap map = null;
private BufferedReader reader = null;
private long lastModifiedTime = 0;
public Reloader(ServletContextEvent event) {
eventCopy = event;
//event.getServletContext().setAttribute("i", new Integer(2));
}

public void run() {
configFilePath = (String)eventCopy.getServletContext().getInitParameter("billRunDetailConfig");
if(configFilePath == null)
{
eventCopy.getServletContext().log("Warning: No bill run detail config file found. Please check the file and restart.");
}
configFile = new File(configFilePath);
if(firstLoad == true)
{
map = createMap(configFile);
lastModifiedTime = configFile.lastModified();
eventCopy.getServletContext().setAttribute("BunRunDetail", map);
eventCopy.getServletContext().log("\n\nFirst load of bill run detail config file. HashMap loaded.\n");
firstLoad = false;
}
else
{
eventCopy.getServletContext().log("\n\nAnother load of bill run detail config file. Checking for the file's last modified time...\n");
if(configFile.lastModified() != lastModifiedTime)
{
map = createMap(configFile);
lastModifiedTime = configFile.lastModified();
eventCopy.getServletContext().setAttribute("BunRunDetail", map);
eventCopy.getServletContext().log("Config file changed. HashMap is hashed again.");
}else
{
eventCopy.getServletContext().log("Config file is not changed.");
}
}
}

private HashMap createMap(File configFile){
HashMap map = null;
try{
reader = new BufferedReader(new FileReader(configFile));
}catch(FileNotFoundException ex){
ex.printStackTrace();
}
return map;
}
}

我想向任何方法的调用者抛出任何异常,但我不能,例如:我可以这样做:

private HashMap createMap(File configFile) throws FileNotFoundException{
HashMap map = null;
try{
reader = new BufferedReader(new FileReader(configFile));
}catch(FileNotFoundException ex){
//ex.printStackTrace();
throw ex;
}
return map;
}

但在上面:

if(firstLoad == true)
{
map = createMap(configFile);
lastModifiedTime = configFile.lastModified();
eventCopy.getServletContext().setAttribute("BunRunDetail", map);
eventCopy.getServletContext().log("\n\nFirst load of bill run detail config file. HashMap loaded.\n");
firstLoad = false;
}

map = createMap(configFile)行中,我在eclipse中提示未处理异常错误,但我只能向其添加一个try catch子句。

我希望它向调用者抛出异常,并让调用者处理异常,因为如果我返回这里,我不确定是否所有资源都已关闭。

<小时/>

为什么它的签名不包含 throws 子句?它是否假设它不会产生任何异常?

如果出现异常,并且我用 try/catch 子句包装它,线程会停止并通知父级吗?

最佳答案

不能在 run() 方法中添加“throws”子句的原因是 run() 是在 Runnable 接口(interface)中定义的,并且其签名不包含 throws 子句。

关于java - 为什么不能声明抛出异常类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5812764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com