以上就是给各位分享Java中的Error和Exception,其中也会对java中的error和exception的区别进行解释,同时本文还将给你拓展-java.io.IOException:Crea
以上就是给各位分享Java 中的 Error 和 Exception,其中也会对java中的error和exception的区别进行解释,同时本文还将给你拓展- java.io.IOException: CreateProcess error=2、Android:FATAL EXCEPTION:AdWorker#1 java.lang.NoSuchMethodError:java.io.IOException.、Caused by: java.io.IOException: CreateProcess error=5, 拒绝访问。、DWR 报错:Error: java.lang.SecurityException, Session Error 和 Allocate exception for servlet 问题的解决办法等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:- Java 中的 Error 和 Exception(java中的error和exception的区别)
- - java.io.IOException: CreateProcess error=2
- Android:FATAL EXCEPTION:AdWorker#1 java.lang.NoSuchMethodError:java.io.IOException.
- Caused by: java.io.IOException: CreateProcess error=5, 拒绝访问。
- DWR 报错:Error: java.lang.SecurityException, Session Error 和 Allocate exception for servlet 问题的解决办法
Java 中的 Error 和 Exception(java中的error和exception的区别)
Exception 和 Error 都是继承了 Throwable 类,在 Java 中只有 Throwable 类型的实例才可以被抛出(throw)或者捕获(catch),它是异常处理机制的基本组成类型。
Exception 和 Error 体现了 Java 平台设计者对不同异常情况的分类。
Exception 是程序正常运行中,可以预料的意外情况,可能并且应该被捕获,进行相应处理。
Error 是指在正常情况下,不大可能出现的情况,绝大部分的 Error 都会导致程序(比如 JVM 自身)处于非正常的、不可恢复状态。既然是非正常情况,所以不便于也不需要捕获,常 见的比如 OutOfMemoryError 之类,都是 Error 的子类。
Exception 又分为可检查(checked)异常和不检查(unchecked)异常,可检查异常在源代码里必须显式地进行捕获处理,这是编译期检查的一部分。
不可查 的 Error,是 Throwable 不是 Exception。 不检查异常就是所谓的运行时异常,类似 NullPointerException、ArrayIndexOutOfBoundsException 之类,通常是可以编码避免的逻辑错误,具体根据需要来判断是否需要捕 获,并不会在编译期强制要求。
Error 和 Exception 的联系
继承结构:Error 和 Exception 都是继承于 Throwable,RuntimeException 继承自 Exception。
Error 和 RuntimeException 及其子类称为未检查异常(Unchecked exception),其它异常成为受检查异常(Checked Exception)。
Error 和 Exception 的区别
Error 类一般是指与虚拟机相关的问题,如系统崩溃,虚拟机错误,内存空间不足,方法调用栈溢出等。如 java.lang.StackOverFlowError 和 Java.lang.OutOfMemoryError。对于这类错误,Java 编译器不去检查他们。对于这类错误的导致的应用程序中断,仅靠程序本身无法恢复和预防,遇到这样的错误,建议让程序终止。
Exception 类表示程序可以处理的异常,可以捕获且可能恢复。遇到这类异常,应该尽可能处理异常,使程序恢复运行,而不应该随意终止异常。
异常处理的两个基本原则
第一,尽量不要捕获类似 Exception 这样的通用异常,而是应该捕获特定异常,在这里是 Thread.sleep () 抛出的 InterruptedException。 这是因为在日常的开发和合作中,我们读代码的机会往往超过写代码,软件工程是门协作的艺术,所以我们有义务让自己的代码能够直观地体现出尽量多的信息,而泛泛 的 Exception 之类,恰恰隐藏了我们的目的。另外,我们也要保证程序不会捕获到我们不希望捕获的异常。比如,你可能更希望 RuntimeException 被扩散出来,而不是被捕获。 进一步讲,除非深思熟虑了,否则不要捕获 Throwable 或者 Error,这样很难保证我们能够正确程序处理 OutOfMemoryError。
第二,不要生吞(swallow)异常。这是异常处理中要特别注意的事情,因为很可能会导致非常难以诊断的诡异情况。 生吞异常,往往是基于假设这段代码可能不会发生,或者感觉忽略异常是无所谓的,但是千万不要在产品代码做这种假设! 如果我们不把异常抛出来,或者也没有输出到日志(Logger)之类,程序可能在后续代码以不可控的方式结束。没人能够轻易判断究竟是哪里抛出了异常,以及是什么原因产生了异 常。
OutOfMemoryError 内存溢出一般是出现在申请了较多的内存空间没有释放的情形。
//java.lang.OutOfMemoryError -Xmx150m
try {
byte[] b = new byte[1024*1024*600];
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
运行时,设置 jvm 最大的 heap 内存 150m,此时申请 600m 的内存,因此会报 error
java.lang.OutOfMemoryError: Java heap space
java.lang.StackOverflowError
堆栈溢出错误。当一个应用递归调用的层次太深而导致堆栈溢出时抛出该错误。
public static void main(String[] args) {
method();
}
public static void method() {
while (true) {
method();
}
}
无限次的递归调用出现
Exception in thread "main" java.lang.StackOverflowError
常见的异常;
ArrayIndexOutOfBoundsException 数组下标越界异常,
ArithmaticException 算数异常 如除数为零
NullPointerException 空指针异常
IllegalArgumentException 不合法参数异常
- java.io.IOException: CreateProcess error=2
androidStudio这个错误提示很模糊,没有任何有价值的信息,几番百度和google后,偶尔发现了一个git的问题,于是重新安装GIT后并配置AS的GIT后,并重启AS,后来问题解决!!
Android:FATAL EXCEPTION:AdWorker#1 java.lang.NoSuchMethodError:java.io.IOException.
FATAL EXCEPTION: AdWorker #1 java.lang.NoSuchMethodError: java.io.IOException.<init> at com.google.android.gms.internal.g.f(UnkNown Source) at com.google.android.gms.internal.g.b(UnkNown Source) at com.google.android.gms.internal.e.a(UnkNown Source) at com.google.android.gms.internal.e.a(UnkNown Source) at com.google.android.gms.internal.bq.ac(UnkNown Source) at com.google.android.gms.internal.cg$1.run(UnkNown Source) at com.google.android.gms.internal.ch$1.run(UnkNown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561) at java.lang.Thread.run(Thread.java:1096)
除了它与谷歌广告/云服务有关,我不知道它来自哪里.直到昨天我才重现它,我启动了一个10英寸的模拟器并立即看到了问题.删除部分代码后,我认为以下部分是问题所在.
public void onActivityCreated(Bundle savedInstanceState) { ............ AdView adView = (AdView)getActivity().findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .addTestDevice("CED2A4FD2C192C08557081CC37AA9E54") .build(); adView.loadAd(adRequest); }
奇怪的是它只发生在10英寸或更高的设备上.我测试了各种不同的设备/版本.该问题仅发生在10英寸设备或更大的设备上. Android版本并不重要. 2.2或4.4它不断崩溃.
根据我的理解,我没有针对特定设备尺寸的任何不同布局文件夹.
我的广告定义的xml如下所示:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" android:id="@+id/mainContainer" android:layout_width="fill_parent" android:layout_height="fill_parent" android:fillViewport="true" android:orientation="vertical" > <LinearLayout android:id="@+id/titleContainer" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > </LinearLayout> <com.viewpagerindicator.TabPageIndicator android:id="@+id/indicator"android:layout_width="fill_parent" android:layout_height="wrap_content" /> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" /> <LinearLayout android:id="@+id/adViewContainer" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal" > <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto" android:id="@+id/adView" android:layout_width="fill_parent" android:layout_height="wrap_content" ads:adUnitId="@string/adid" ads:adSize="SMART_BANNER" android:gravity="bottom" /> </LinearLayout> </LinearLayout>
我尝试了不同的横幅尺寸.没有帮助.删除广告请求代码可以解决问题,但也会删除广告.
谁有线索?
解决方法
http://developer.android.com/google/play-services/setup.html
Note: Google Play services 4.0.30 (released November 2013) and newer
versions require Android 2.3 or higher. If your app supports Android
2.2,you can continue development with the Google Play services SDK,but must instead install Google Play services for Froyo from the SDK
Manager.
http:// android-developers.blogspot.no/2013/10/google-play-services-40.html
With over 97% of devices Now running Android 2.3 (Gingerbread) or
newer platform versions,we’re dropping support for Froyo from this
release of the Google Play services SDK in order to make it possible
to offer more powerful APIs in the future. That means you will not be
able to utilize these new APIs on devices running Android 2.2 (Froyo).
https://developer.android.com/google/play-services/setup.html#ensure
Important: Because it is hard to anticipate the state of each device,
you must always check for a compatible Google Play services APK before
you access Google Play services features. For many apps,the best time
to check is during the onResume() method of the main activity.
Alernative 1用于在使用AdView之前检查Google Play服务是否可用.
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext(); if(status == ConnectionResult.SUCCESS) { //Success! Do what you want }
Alernative 2是使用GoogleAdMobAdsSdk-6.4.1.jar
Caused by: java.io.IOException: CreateProcess error=5, 拒绝访问。
今天在用java调用按Android ADB时进行执行时发现了一个错误,也就是下面这个错误。
Caused by: java.io.IOException: CreateProcess error=5, 拒绝访问。
我的访问路径是这个
刚开始我以为是权限不够,就去修改文件或者系统的一些权限,然而没什么用,将文件换到别的盘也照样报错,后来查看了很多资料,看到一个令我感到羞愧的答案,其实这个问题不应该犯的
解决:读取的如果是文件夹,就会出此错误,读取文件夹,无论你怎么读,都无法读取,读取的目录后面忘加了文件名!
DWR 报错:Error: java.lang.SecurityException, Session Error 和 Allocate exception for servlet 问题的解决办法
DWR Session Error问题的解决办法
来源于百度文库:http://wenku.baidu.com/link?url=RltL4aB3-To1EDI-mG3K7Vz7LdFWERxdu0Mpw5f29nd59pw_-qUHwB1HGZcRCjfMsUXVbgZeYxaD0JN6_HPz9tay7Z2V6DN5F58dcq8QF37
问题情境再现 使用的Dwr版本2.0
在一台服务器上的不同端口上部署了同样的程序(tomcat5.5.28 80端口,tomcat 5.5.28 8080端口)
使用浏览器先后登陆80,8080端口的程序,都不注销,保持会话状态。
然后浏览器切换到8080的一个使用了DWR ajax功能的页面上,浏览器弹出Session Error的提示。
但是,如果切换到80端口的程序上,同样进入到一个使用了dwr ajax技术的页面上,没有Session Error的提 示。
问题诊断:
初步怀疑浏览器的问题。
检查浏览器的cookie中的jsessionid的值。因为我们知道,Http协议本身是无状态的,服务器标识同一次会话的过程就是借助于cookie中的某个值或者通过url重写的方式来实现。这也是jsp程序的session原理。
检查发现:cookie中存在2个sessionid项,sessionid的值不同。
因为站点地址相同,url也相同(除了端口不同外),因此,浏览器“误”认为是同一个程序,把缓存的cookie项都发送回了服务器。
然后再观测ajax请求的值,即http post或get的参数值如下: callCount=1
page=/web/initRolePermission.action
httpSessionId=3F5F7D7C14D40667FF126DC6F9038EE5 scriptSessionId=5B2B53E512648E78C92393E052589CA3859 c0-scriptName=adminRolePerAction c0-methodName=findPermission c0-id=0
c0-param0=string:181 batchId=0
在这里,务必注意
httpSessionId=3F5F7D7C14D40667FF126DC6F9038EE5,
实际上,一般情况下,httpSessionId和cookie中的jsession值是相同的。
至于dwr组件中,为什么要加上httpSessionId,这是因为dwr开发团队考虑到了跨站攻击问题。因此,通过验证dwr ajax请求中的httpSessionId值, 来防止跨站攻击。
在重现,诊断问题过程中,发现Session Error的信息是来自dwr ajax请求的响应中,抛出的异常是java.lang.SecurityException,因此可以怀疑这个错误信息是源于dwr源代码中的。
用Eclipse打开下载到的dwr源代码。搜索Session Error的信息,然后在org.directwebremoting.dwrp.Batch 类中找到了,其部分代码如下: /**
* Check that this request is not subject to a CSRF attack * @param request The original browser's request
* @param sessionCookieName "JSESSIONID" unless it has been overridden */
private void checkNotCsrfAttack(HttpServletRequest request,String sessionCookieName) {
// A check to see that this isn't a csrf attack
// http://en.wikipedia.org/wiki/Cross-site_request_forgery // http://www.tux.org/~peterw/csrf.txt if (request.isRequestedSessionIdValid() && request.isRequestedSessionIdFromCookie()) {
String headerSessionId = request.getRequestedSessionId(); if (headerSessionId.length() > 0) {
String bodySessionId = getHttpSessionId();
// normal case; if same session cookie is supplied by DWR and // in HTTP header then all is ok
if (headerSessionId.equals(bodySessionId)) { return; }
// Weblogic adds creation time to the end of the incoming
// session cookie string (even for request.getRequestedSessionId()). // Use the raw cookie instead
Cookie[] cookies = request.getCookies(); for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookie.getName().equals(sessionCookieName) && cookie.getValue().equals(bodySessionId)) { return; } }
// Otherwise error
log.error("A request has been denied as a potential CSRF attack."); throw new SecurityException("Session Error"); } } }
仔细分析这段代码,即使在上述问题情境环境中,也不会出现Session Error的错误。 然后反编译正在使用的dwr类文件,找到batch类,代码却不同,代码如下: private void checkNotCsrfAttack(HttpServletRequest request) {
if(request.isRequestedSessionIdValid() && request.isRequestedSessionIdFromCookie()) {
String headerSessionId = request.getRequestedSessionId(); if(headerSessionId.length() > 0) {
String bodySessionId = getHttpSessionId(); if(!bodySessionId.startsWith(headerSessionId)) throw new SecurityException("Session Error"); } } }
该代码没有考虑到cookie中出现多个jsession的情况。
到此,问题就发现并解决。
由于dwr低版本的bug引起的,升级dwr版本即解决此问题。 可能,读者还有一个疑问:为什么该问题只出现在8080端口上。
检查发现:80端口的cookie项(就是80端口产生的jsessionid出现在cookie项的最前面),
这样在80端口上的程序访问中,dwr ajax请求中的httpSessionId和cookie项最前面的jsessionid值相同,在80端口上,就自然不会出现session error错误。
三、
严重: Allocate exception for servlet dwr
org.xml.sax.SAXException: Failed to resolve: arg0=-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN arg1=http://getahead.org/dwr/dwr30.dtd
这是因为添加的jar包是2.0版本的,但是却引用了3.0的dtd文件,所以报错了,改为如下配置即可:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEdwrPUBLIC"-//GetAheadLimited//DTDDirectWebRemoting2.0//EN"
- "http://www.getahead.ltd.uk/dwr/dwr20.dtd">
- <dwr>
- <allow>
- <createcreator="new"javascript="dwrDate">
- <paramname="class"value="java.util.Date"></param>
- </create>
- </allow>
- </dwr>
关于Java 中的 Error 和 Exception和java中的error和exception的区别的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于- java.io.IOException: CreateProcess error=2、Android:FATAL EXCEPTION:AdWorker#1 java.lang.NoSuchMethodError:java.io.IOException.、Caused by: java.io.IOException: CreateProcess error=5, 拒绝访问。、DWR 报错:Error: java.lang.SecurityException, Session Error 和 Allocate exception for servlet 问题的解决办法的相关知识,请在本站寻找。
本文标签: