我有一个关于在Web应用程序中处理异常的问题.我经常听到抓住超级类异常是一个坏主意.
我经常编写代码来捕获struts action/java servlet类中的所有异常.
try {
// call business facade
// business facade calls DAO
// any exception from DAO bubbles up
} catch (Exception e) {
log.error("error", e);
}
Run Code Online (Sandbox Code Playgroud)
如果我们不捕获超类Exception.我们如何处理任何意外的运行时错误并适当地记录它们
我想知道是否有人可以帮助我理解如何在clojure中执行以下代码:
int[] secondArray = new int[500];
for (int i = 0; i < firstArray.length; i++)
{
secondArray[firstArray[i]] += 1;
}
Run Code Online (Sandbox Code Playgroud) 我正在使用java中的属性类为我的应用程序保存一些配置.这是我第一次使用房产,所以请温柔地跟我说:)
我可以从属性中插入和检索数据,没问题,但我希望将数据插入如下:
属性文件:
#Header generated by java ~ this is fine, I don't care
#Server 1 configuration
url=192.168.1.1
port=6546
username=max
password=123
#Server 2 configuration
url=192.168.2.1
port=6454
username:dude
password:123
#And so on...
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
public void setProp(String host, String port, String user, String pass,
String host2, String port2, String user2, String pass2)
{
try{
prop.setProperty("host", host);
prop.setProperty("port", porto);
prop.setProperty("username", user);
prop.setProperty("password", pass);
prop.setProperty("host2", host2);
prop.setProperty("port2", porto2);
prop.setProperty("username2", user2);
prop.setProperty("password2", pass2);
config.store(new FileOutputStream("configuration.properties"), "Server 1 Configuration");
}catch (Exception e) {
JOptionPane.showMessageDialog(null,"Error: "+e.getMessage());
} …Run Code Online (Sandbox Code Playgroud) 我遇到的问题是我想使用SAX defaultHandler类的扩展来解析出1个XML文档.当只使用一个处理程序时,我可以解析XML并为对象的属性分配不同的标记(请参阅域和区域).一旦我在域和区域对象中有这些,我想将它们添加到另一个具有域和区域列表的对象(GroupedFiles).我遇到的问题是使用2个处理程序解析出1个文档.我的理论,尽管不是最好的编码实践,是解析文档两次,使用域处理程序运行一次,设置域,然后添加到分组文件,然后对区域执行相同操作.这是代码,
GroupedFiles groupedFiles = new GroupedFiles();
ArrayList<Domain> domains = new ArrayList<Domain>();
ArrayList<Area> areas = new ArrayList<Area>();
//Create parser from factory
XMLReader parser = XMLReaderFactory.createXMLReader();
//Creates an input stream from the file "someFile.xml"
InputStream in = new FileInputStream(new File("someFile.xml"));
InputSource source = new InputSource(in);
//Create handler instances
DomainHandler domainHandler = new DomainHandler();
AreaHandler areaHandler = new AreaHandler();
//Parses out XML from a document using each handler,
//adding it to an object with the correct properties then adds those
//to another …Run Code Online (Sandbox Code Playgroud) 我遇到了以下java代码,我不确定它是什么意思.我们可以在实例化一个类之后在'{'中编写代码,例如新的TestClass {*/code goes here*/}
但是当我尝试运行代码时,我没有在输出中看到"Z是10".有人可以给我一些链接,我可以得到一些与java的这个功能相关的更多信息.
class TestClass {
int z;
public TestClass(){
z=10;
}
public int getZ(){
return z;
}
public void setZ(int z){
this.z=z;
}
}
class A
{
public static void main (String[] args) throws java.lang.Exception
{
TestClass TC = new TestClass() {
public void testprint() {
System.out.println("Z is " + getZ());
}
};
}
}
Run Code Online (Sandbox Code Playgroud) 我知道一些Thread方法清除了中断的标志(例如,睡眠,等待......).但为什么他们这样做?它是什么原因?
在Java并发类中,我建议在多线程应用程序中使用以下代码作为计数器
private volatile int count;
Run Code Online (Sandbox Code Playgroud)
我问自己是否可以将volatile关键字与包装类Integer一起使用而不是原始类型int(见下文):
private volatile Integer count;
Run Code Online (Sandbox Code Playgroud)
在这种情况下使用Integer包装类是否正确?
我有一个cron工作来清除超过某个月数(由用户设置)的数据.cron作业调用一个purgeData()方法来清除postgres表中的数据.我Calendar从当前日期(via GregorianCalendar.getInstance)操作java 来确定删除之前数据的目标日期.
我的问题是日历操作和/或将新操作的日历转换为字符串以便在postgres中使用偶尔会失败,将目标日期设置为删除当前日期之前的所有内容的当前日期,而不是早于1的数据(或#数月保持数月).
这是我简单的日期格式:
public static final SimpleDateFormat dateFormatter = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss.SSS");
Run Code Online (Sandbox Code Playgroud)
这是我的方法:
public String purgeData() throws ParseException {
Connection con = null;
String sqlString = "";
PreparedStatement pst = null;
String returnString = "";
Calendar startDate = GregorianCalendar.getInstance();
returnString += "# Months to keep data: " + getNumMonthsKeepData();
startDate.add(Calendar.MONTH, getNumMonthsKeepData() * -1);
String targetDate = dateFormatter.format(startDate.getTime());
Calendar today = GregorianCalendar.getInstance();
returnString +=" Target date (string): " + targetDate + " start …Run Code Online (Sandbox Code Playgroud) 传递多个字符串进行日志记录时,最好使用varargs还是字符串连接?
将在生产环境中禁用日志记录.
考虑以下代码,
public void log(int logLevel, String ... msg) {
if (logLevel >= currentLogLevel) {
for (String m : msg) {
// print the log to file
}
}
}
// calling
log(2, "text1", var1, "text2");
Run Code Online (Sandbox Code Playgroud)
比.
public void log(int logLevel, String msg) {
if (logLevel >= currentLogLevel) {
// print the log to file
}
}
// calling
log(2, "text1" + var1 + "text2");
Run Code Online (Sandbox Code Playgroud)
哪一个在两种情况下都有效(启用/禁用)?
我正在构建一个Flask应用程序,该应用程序应该将列表中的所有项目作为HTML页面中的新行返回.
例如:
list = [1,2,3,4]
Run Code Online (Sandbox Code Playgroud)
我想list在HTML页面中将每个项目打印为一个新段落,如下所示:
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
Run Code Online (Sandbox Code Playgroud) java ×8
calendar ×1
clojure ×1
concurrency ×1
exception ×1
flask ×1
logging ×1
performance ×1
properties ×1
python-2.7 ×1
sax ×1
saxparser ×1
sleep ×1
static ×1
xml ×1
xml-parsing ×1