Java中的异常处理机制的简单原理?

答:

   1java常见异常架构图

   

 

 

 1、  粉红色的是受检查异常(checked exception),其必须被try{}catch语句块所捕获,或者在方法签名里通过throws子句声明抛出 因为Java 编译器要进行检查,Java虚拟机也要进行检查,以确保遵守这个规则,如果不进行处理会报错

运行时异常(runtime exception),需要程序员自己分析代码决定是否捕获和处理。

Error,属于严重错误,一般指与虚拟机相关的问题,如系统崩溃、虚拟机报错、动态链接失败等。无法通过程序编译时进行捕获和处理,需要根据业务信息进行特殊处理,Error不需要捕获。

2、 try{}里有一个return语句,那么紧跟在try后的finally{},先于return执行。只有try{}语句中使用System.exit(),时finally语句才不会被执行,因为系统直接退出。另,try{}语句和Finally{}中同时包含return()时,try()中的return()将不会被执行,只执行finally语句的return()语句。

3、 当时用try{}catch{}语句时,异常是如何寻找catch块的呢,每一个catch块都有一个指定的异常类及其子类的异常实例。当try{}中语句产生异常后,try{}其抛出到JVM运行环境中,运行环境接收到该异常后会判断该异常由哪个catch块处理。当未找到可以处理该异常的catch块时,程序运行终止,系统抛出异常。Java7中有多异常捕获,多异常类型使用“|”隔开,多异常捕获的异常变量使用隐式final修饰,因此变量名不可以改变。

多异常捕获:

try{

}catch(IndexOutOfBoundsException|NumberFormatException  ie){

//ie变量默认有final修饰,因此下面语句就会报错

ie=new NumberFormatException("test");

}

4、 throwsthrow

checked exception中,throws为当不知道异常为何种异常,或者不想对异常进行处理时,需要进行声明抛出异常throw为用户自行处理抛出异常,多数用于向用户提示信息。

5catchthrow同时使用,有些异常比较复杂,一catch块不能将其处理完全,这时需要在catch中自定义一个throw抛出异常,告诉调用该方法的调用者需要对这个异常再次进行处理。

 

EOFException

You can get a EOFException java.io.EOFException in two main ways 

EOFException during FileSystem operations

Unless this is caused by a network issue (see below), and EOFException means that the program working with a file in HDFS or another supported FileSystem has tried to read or seek beyond the limits of the file. 

There is an obvious solution here: don't do that. 

EOFException during Network operations

You can see an EOFException during network operations, including RPC calls between applications talking to HDFS, the JobTracker, YARN services or other Hadoop components. 

It can mean 

Unexpected Server shutdown

The far end of the network link shut down during the RPC operation. 

1. Verify that the server at the end of the network operation is running -restart it if not. 

2. If the service is an HA component, it may be that failover has occurred -but the client doesn't detect this and retry its operation. Try restartomg the application. 

Protocol Mismatch

There is some protocol mismatch between client and server which means that the server sent less data than the client expected. This is rare in the core Hadoop components, as the RPC mechanisms used versioned protocols precisely to prevent versioning problems. It is more likely in a third-party component, or a module talking to a remote filesystem. 

1. Retry the operation, it may work this time. 

   2.Look at the stack trace and see if it occurs in a Hadoop class (org.apache.hadoop -especially an RPC one), or something else. 

3.If it happens in one of the Hadoop remote filesystems (s3, s3n, ftp ...etc.), or in Apache HTTP libraries, it usually means the far end has finished early. Try again. 

Attention: Developers of RPC clients

If your users see this a lot, it implies it is time to make your client applications use the org.apache.hadoop.io.retry package to make them more resilient to outages

API中解释: 

Thrown when a program encounters the end of a file or stream during an input operation.

EOFException

  大概意思:

      EOFException在两种情况会发生;

      1、文件引发异常(filesystem),读取、查找、输入时超出文件限制,文件或者流突然结束,引发异常

      2、网络访问引发异常(NetWork):

                    A 当服务端已经关闭,客户继续访问

                    B 服务端具有防护墙,客户端继续访问

                    C 客户端访问服务端,但协议不正确

      

ava.net.MalformedURLException: unknown protocol: c

这个错一般有两种原因导致:

1URL协议、格式或者路径错误, 好好检查下你程序中的代码

如果是路径问题,最好不要包含中文路径,因为有时中文路径会乱码,导致无法识别

 

2jar问题:用jdom解析xml文件,如果应用路径里有gnujaxp.jar包的话,jdom就会  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值