EXCEPTIONS
An Example of Exceptions
11.4
try {
thrower(args[i]);
System.out.println("Test \"" + args[i] +
"\" didn t throw an exception");
} catch (Exception e) {
System.out.println("Test \"" + args[i] +
"\" threw a " + e.getClass() +
"\n
with message: " + e.getMessage());
}
}
}
static int thrower(String s) throws TestException {
try {
if (s.equals("divide")) {
int i = 0;
return i/i;
}
if (s.equals("null")) {
s = null;
return s.length();
}
if (s.equals("test"))
throw new TestException("Test message");
return 0;
} finally {
System.out.println("[thrower(\"" + s +
"\") done]");
}
}
}
If we execute the test program, passing it the arguments:
divide null not test
it produces the output:
[thrower("divide") done]
Test "divide" threw a class java.lang.ArithmeticException
with message: / by zero
[thrower("null") done]
Test "null" threw a class java.lang.NullPointerException
with message: null
[thrower("not") done]
Test "not" didn t throw an exception
[thrower("test") done]
Test "test" threw a class TestException
with message: Test message
207






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

java hosting

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Inexpensive Web Hosting  Jsp Web Hosting

Cheapest Web Hosting Jsp Hosting Cheap Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved