GVKun编程网logo

JAVA-Exception&Error

27

如果您对JAVA-Exception&Error感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解JAVA-Exception&Error的各种细节,此外还有关于-java.io.IOExcept

如果您对JAVA-Exception&Error感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解JAVA-Exception&Error的各种细节,此外还有关于- 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-Exception&Error

JAVA-Exception&Error

JAVA——Exception&Error

在万物皆对象的JAVA中,先让我们看看Exception和Error的地位吧:

avatar

可见Exception和Error都继承自Throwable类,所以二者既有相同也有不同之处。相同之处就是他们都是程序出错导致的,或说程序不正常运行才会实例化的类,不同之处就在于产生他们的的程序的出错原因不同,下面就来详细比较一下。

一,Exception&Error

从命名来看Error貌似比Exception要严重一点,事实确实如此。Error产生的情况一般是指不大可能出现且不可预料的情况,如JVM本身不正常,内存溢出,栈溢出等一些比较严重,会使程序终止的情况。而Exception通常是一些可以预料的不好的情况,如类型转换失败,多线程数据插入失败等,虽然程序出错,但可预料,一般也不会使程序终止。

二,未受检查异常和受检查异常

1,Error和Exception的子类RuntimeException都是未受检查的异常,也可以叫运行时异常,因为它们都是不可预料的,在程序运行时才会出现的异常。

2,Exception的其他子类是受检查的异常,是指程序员可以在代码中预料和捕捉到的程序异常,这也是程序员重点关注的地方。

三,异常处理

要搞清楚异常处理就得先知道try-catch-finally-return这几个关键字的语法和使用,它们在程序中的语法位置如下:

try{
    //可能会产生异常的语句
}catch(Exception e){
    //产生异常后才会执行的代码
}finally{
    //不管有没有产生异常,在执行完上面两个代码块后,此处代码都会执行
}

画个流程图更容易理解,如下:

avatar

*注意:*还有一个return的用法很多人会出错,当try-catch-finally中出现return语句时,要注意代码执行顺序,在代码中讲解,如下:

public int f1() {
		int res=0;
		try {
			res=1;
		}catch(Exception e){	
		}finally {
			res=5;		
		}
		return res;
	}
/*上面这段代码的执行结果按照前面的流程图推一下很容易知道f1()方法的返回值是5
但如果是下面这种代码形式呢
*/
public int f2() {
		int res=0;
		try {
			res=1;
            return res;
		}catch(Exception e){	
		}finally {
			res=5;		
		}
		return res;
	}
/*f2()的返回值应该是1,因为在try中执行到return语句时程序会先将要return的值入临时栈,此时finally中执行的操作改变res的值,但却不会影响到存在临时栈中return的值。
再看下面这种情况
*/
public int f3() {
		int res=0;
		try {
			res=1;
			return res;
		}catch(Exception e){	
		}finally {
			res=5;
			return res;
		}
	}
/*此时f3()返回值应该是5,因为程序会把finally中的return值替换掉try中的return值
*/

四,throw & throws

throw : 将产生的异常抛出(强调的是动作),抛出的既可以是异常的引用,也可以是异常对象。(位置: 方法体内

throws : 如果一个方法可能会出现异常,但没有能力处理这种异常,可以在方法声明处用throws子句来声明抛出异常。用它修饰的方法向调用者表明该方法可能会抛出异常(可以是一种类型,也可以是多种类型,用逗号隔开)(位置: 写在方法名 或方法名列表之后 ,在方法体之前。)

五,自定义异常

可以继承Exception或Throwable类来定义自己的异常类型

public class MyException extends Exception{
	public MyException() {
		
	}
	public MyException(String msg) {
		super(msg);
	}
}

异常抛出方法和自带异常一样,如下

public int exceptionTest(int a) throws MyException {
		if(a<10) {
			System.out.println("程序正常");
			return a;
		}
		else {
			throw new MyException("程序异常");
		}
	}

以及捕获异常如下

public static void main(String[] args) {	
		Main m=new Main();
		try {
			int res=m.exceptionTest(11);
		} catch (MyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

- java.io.IOException: CreateProcess error=2

- java.io.IOException: CreateProcess error=2

androidStudio这个错误提示很模糊,没有任何有价值的信息,几番百度和google后,偶尔发现了一个git的问题,于是重新安装GIT后并配置AS的GIT后,并重启AS,后来问题解决!!

Android:FATAL EXCEPTION:AdWorker#1 java.lang.NoSuchMethodError:java.io.IOException.

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>

我尝试了不同的横幅尺寸.没有帮助.删除广告请求代码可以解决问题,但也会删除广告.
谁有线索?

解决方法

Google移动广告现在通过Google Play Services 4.0提供,但由于Google Play服务不可用而出现此错误.很可能你会在Froyo遇到这个问题,那里有一个可以使用的特殊版本.

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, 拒绝访问。

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 报错: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文件,所以报错了,改为如下配置即可:

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <!DOCTYPEdwrPUBLIC"-//GetAheadLimited//DTDDirectWebRemoting2.0//EN"
  3. "http://www.getahead.ltd.uk/dwr/dwr20.dtd">
  4. <dwr>
  5. <allow>
  6. <createcreator="new"javascript="dwrDate">
  7. <paramname="class"value="java.util.Date"></param>
  8. </create>
  9. </allow>
  10. </dwr>

今天的关于JAVA-Exception&Error的分享已经结束,谢谢您的关注,如果想了解更多关于- 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 问题的解决办法的相关知识,请在本站进行查询。

本文标签: