Предложение от Vivek Pabby
Следующий кусок кода должен помочь:
// This is the calling method
public void yourCallingMethod()
{
// some code ........
try {
yourCalledMethod();
}
// This catch will catch the thrown exception from the called method
catch (IOException e1) {
showStatus("Caught thrown Exception : " + e1.getMessage() );
}
}
public void yourCalledMethod()
{
//
// The following try/catch block will catch IOException and throw it again
//
try {
DataInputStream din = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));
DataOutputStream dout = new DataOutputStream(
new BufferedOutputStream(socket.getOutputStream()));
}
catch(IOException e) {
showStatus("Exception caught: " + e.getMessage() );
//
// By throwing the exceptiuon again, you can have the calling method catch it.
//
throw e;
}
}
2. Как определить границы исключений, которые защитят весь код?
Предложение от Vivek Pabby
Любой код в блоке try-catch передаст ичключение этому блоку. Если вы поместите весь ваш код в главный блок try-catch вы можете перехватить любое непойманное исключение от этого кода, вот пример:
try {
// put all your code here
// this code can also have its own try-catch blocks
// making it a nested try-catch situation
try {
// more code .......
} catch (anyExceptionYouLike e) {}
// more code.......
}
// This catch will catch any exceptions in all your blocked code which are not
// caught by any inner try-catch blocks
catch (ArrayIndexOutOfBounds ex) {
showStatus("Exception : " + ex.getMessage() );
}
catch (IOException e1) {
showStatus("Exception : " + e1.getMessage() );
} catch (Exception e2) { }
}
Вы так же можете создавать свои собственные исключения с определенными сообщениями и обрабатывать и ловить их .
Следующий кусок кода должен помочь:
// This is the calling method
public void yourCallingMethod()
{
// some code ........
try {
yourCalledMethod();
}
// This catch will catch the thrown exception from the called method
catch (IOException e1) {
showStatus("Caught thrown Exception : " + e1.getMessage() );
}
}
public void yourCalledMethod()
{
//
// The following try/catch block will catch IOException and throw it again
//
try {
DataInputStream din = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));
DataOutputStream dout = new DataOutputStream(
new BufferedOutputStream(socket.getOutputStream()));
}
catch(IOException e) {
showStatus("Exception caught: " + e.getMessage() );
//
// By throwing the exceptiuon again, you can have the calling method catch it.
//
throw e;
}
}
2. Как определить границы исключений, которые защитят весь код?
Предложение от Vivek Pabby
Любой код в блоке try-catch передаст ичключение этому блоку. Если вы поместите весь ваш код в главный блок try-catch вы можете перехватить любое непойманное исключение от этого кода, вот пример:
try {
// put all your code here
// this code can also have its own try-catch blocks
// making it a nested try-catch situation
try {
// more code .......
} catch (anyExceptionYouLike e) {}
// more code.......
}
// This catch will catch any exceptions in all your blocked code which are not
// caught by any inner try-catch blocks
catch (ArrayIndexOutOfBounds ex) {
showStatus("Exception : " + ex.getMessage() );
}
catch (IOException e1) {
showStatus("Exception : " + e1.getMessage() );
} catch (Exception e2) { }
}
Вы так же можете создавать свои собственные исключения с определенными сообщениями и обрабатывать и ловить их .