当输入期间意外终止文件或流时,将抛出“EOFException”。 以下是抛出EOFException异常的一个示例,来自JavaBeat应用程序:
import java.io.DataInputStream;import java.io.EOFException;import java.io.File;import java.io.FileInputStream;import java.io.IOException;public class ExceptionExample { public void testMethod1() {
File file = new File("test.txt");
DataInputStream dataInputStream = null; try {
dataInputStream = new DataInputStream(new FileInputStream(file)); while (true) {
dataInputStream.readInt();
}
} catch (EOFException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally { try { if (dataInputStream != null) {
dataInputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} public static void main(String[] args) {
ExceptionExample instance1 = new ExceptionExample();
instance1.testMethod1();
}
}
运行上面的程序,将抛出以下异常:
java.io.EOFExceptionat java.io.DataInputStream.readInt(DataInputStream.java:392)at logging.simple.ExceptionExample.testMethod1(ExceptionExample.java:16)at logging.simple.ExceptionExample.main(ExceptionExample.java:36)
当DataInputStream类尝试在流中读取数据但没有更多数据时,将抛出“EOFException”。它也可以发生在ObjectInputStream和RandomAccessFile类中。
阅读关于运行Java软件时可能发生“EOFException”的讨论。(@StackOverflow)
当不支持字符编码时,会抛出此Java软件错误消息(@Penn)。
public UnsupportedEncodingException()
正在使用的Java虚拟机可能不支持给定的字符集。
阅读关于如何在运行Java软件时处理“UnsupportedEncodingException”异常的讨论。(@StackOverflow)
“SocketException”异常表示创建或访问套接字时出错(@ProgramCreek)。
public void init(String contextName, ContextFactory factory) { super.init(contextName, factory);
String periodStr = getAttribute(PERIOD_PROPERTY); if (periodStr != null) { int period = 0; try {
period = Integer.parseInt(periodStr);
} catch (NumberFormatException nfe) {} if (period <= 0) { throw new MetricsException("Invalid period: " + periodStr);
}
setPeriod(period);
}
metricsServers =
Util.parse(getAttribute(SERVERS_PROPERTY), DEFAULT_PORT);
unitsTable = getAttributeTable(UNITS_PROPERTY);
slopeTable = getAttributeTable(SLOPE_PROPERTY);
tmaxTable = getAttributeTable(TMAX_PROPERTY);
dmaxTable = getAttributeTable(DMAX_PROPERTY); try {
datagramSocket = new DatagramSocket();
} catch (SocketException se) {
se.printStackTrace();
}
}
当由于以下原因而达到最大连接时,通常会抛出此异常:
阅读关于如何在运行Java软件时解决“SocketException”问题的讨论。(@StackOverflow)
此Java软件错误消息发生在与SSL相关的操作出现故障的时候。 以下示例来自Atlassian(@Atlassian):
com.sun.jersey.api.client.ClientHandlerException: javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
at com.sun.jersey.client.apache.ApacheHttpClientHandler.handle(ApacheHttpClientHandler.java:202)
at com.sun.jersey.api.client.Client.handle(Client.java:365)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:556)
at com.sun.jersey.api.client.WebResource.get(WebResource.java:178)
at com.atlassian.plugins.client.service.product.ProductServiceClientImpl.getProductVersionsAfterVersion(ProductServiceClientImpl.java:82)
at com.atlassian.upm.pac.PacClientImpl.getProductUpgrades(PacClientImpl.java:111)
at com.atlassian.upm.rest.resources.ProductUpgradesResource.get(ProductUpgradesResource.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.atlassian.plugins.rest.common.interceptor.impl.DispatchProviderHelper$ResponseOutInvoker$1.invoke(DispatchProviderHelper.java:206)
at com.atlassian.plugins.rest.common.interceptor.impl.DispatchProviderHelper$1.intercept(DispatchProviderHelper.java:90)
at com.atlassian.plugins.rest.common.interceptor.impl.DefaultMethodInvocation.invoke(DefaultMethodInvocation.java:61)
at com.atlassian.plugins.rest.common.expand.interceptor.ExpandInterceptor.intercept(ExpandInterceptor.java:38)
at com.atlassian.plugins.rest.common.interceptor.impl.DefaultMethodInvocation.invoke(DefaultMethodInvocation.java:61)
at com.atlassian.plugins.rest.common.interceptor.impl.DispatchProviderHelper.invokeMethodWithInterceptors(DispatchProviderHelper.java:98)
at com.atlassian.plugins.rest.common.interceptor.impl.DispatchProviderHelper.access$100(DispatchProviderHelper.java:28)
at com.atlassian.plugins.rest.common.interceptor.impl.DispatchProviderHelper$ResponseOutInvoker._dispatch(DispatchProviderHelper.java:202)
...Caused by: javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
...Caused by: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
...Caused by: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
发生这种情况的原因有:
阅读关于可能导致Java软件“SSLException”错误的讨论。(@StackOverflow)
当资源丢失时,会发生“MissingResourceException”异常。如果资源在正确的类路径中,那么通常是因为属性文件没有正确配置。下面有一个例子(@TIBCO):
java.util.MissingResourceException: Can't find bundle for base name localemsgs_en_US, locale en_US
java.util.ResourceBundle.throwMissingResourceException
java.util.ResourceBundle.getBundleImpl
java.util.ResourceBundle.getBundle
net.sf.jasperreports.engine.util.JRResourcesUtil.loadResourceBundle
net.sf.jasperreports.engine.util.JRResourcesUtil.loadResourceBundle
阅读关于如何在运行Java软件时修复“MissingResourceException”的讨论。
当Java应用程序想要执行命名操作但无法创建连接时,会发生“NoInitialContextException”异常(@TheASF)。
[java] Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial[java] at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)[java] at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)[java] at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:284)[java] at javax.naming.InitialContext.lookup(InitialContext.java:351)[java] at org.apache.camel.impl.JndiRegistry.lookup(JndiRegistry.java:51)
这解决起来可能会是一个复杂的问题,但这里有一些可能导致“NoInitialContextException”Java错误消息的原因:
阅读关于运行Java软件时“NoInitialContextException”意味什么的讨论。(@StackOverflow)
当迭代(例如“for”循环)尝试访问下一个元素而没有元素的时候,就会出现“NoSuchElementException”异常。
public class NoSuchElementExceptionDemo{ public static void main(String args[]) {
Hashtable sampleMap = new Hashtable();
Enumeration enumeration = sampleMap.elements();
enumeration.nextElement(); //java.util.NoSuchElementExcepiton here because enumeration is empty
}
}
Output:
Exception in thread "main" java.util.NoSuchElementException: Hashtable Enumerator
at java.util.Hashtable$EmptyEnumerator.nextElement(Hashtable.java:1084)
at test.ExceptionTest.main(NoSuchElementExceptionDemo.java:23)
抛出“NoSuchElementException”可能的途径:
阅读此关于如何在Java软件中修复“NoSuchElementException”的教程。(@javinpaul)
当应用程序尝试访问对象中的一个字段,但指定的字段不再存在于对象中时,将抛出此Java软件错误消息(@sourceforge)。
public NoSuchFieldError()
通常,该错误在编译器中被捕获,但是如果在编译和运行之间更改了类定义,则在运行时将被捕获。
阅读此关于如何在运行Java软件时发现什么导致“NoSuchFieldError”的讨论。(@StackOverflow)
当应用程序尝试将字符串转换为数字类型,但该数字不是有效的数字字符串时,会出现此Java软件错误消息(@alvinalexander)。
package com.devdaily.javasamples;public class ConvertStringToNumber { public static void main(String[] args) { try {
String s = "FOOBAR"; int i = Integer.parseInt(s); // this line of code will never be reached
System.out.println("int value = " + i);
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
}
}
可能抛出“NumberFormatException”的原因有:
阅读关于如何在运行Java软件时避免“NumberFormatException”的讨论。(@StackOverflow)。
当阻塞操作超时时,会出现此Java软件错误消息。
private void queueObject(ComplexDataObject obj) throws TimeoutException, InterruptedException { if (!queue.offer(obj, 10, TimeUnit.SECONDS)) {
TimeoutException ex = new TimeoutException("Timed out waiting for parsed elements to be processed. Aborting."); throw ex;
}
}
阅读此关于如何在运行Java软件时处理“TimeoutException”的讨论。(@StackOverflow)。