Professional Documents
Culture Documents
Java
Java
Java
9.1
9.1.1
Java
null
str String
NullPointerException main
ExceptionDemo.java 4
public class ExceptionDemo1{
public static void printLength(){
String str=null;
// null
int strLength=str.length();
//
System.out.println("strLength "+strLength);
}
public static void main(String args[ ]){
y 144 y
9
printLength();
}
}
4 8
9.1.2
Error
Exception
IOException
9-1
RuntimeException
ExceptionException
RuntimeException IOException
RuntimeException
NullPointerException API RuntimeException
null RuntimeException
1IndexOutOfBoundsException
int array1={1,2,3};
array1[3]=9;
3 012 array1[3]
2ArithmeticException
y 145 y
Java
Exception
RuntimeException RuntimeException
RuntimeException
Error
9.2
Java
finally printStatckTrace()
9.2.1
try{
//
}
catch( 1 )
{
// 1
}
catch( 2 )
{
// 2
}
catch( n )
{
// n
}
try-catch try
try
try catch catch try
9
int strLength=0;
try
{
strLength=str.length();
}
catch(NullPointerException e)
{
System.out.println("");
}
System.out.println("strLength "+strLength);
}
}
strLength 0
catch
try catch
ArrayIndexOutOfBoundsException ArrayIndexOutOfBoundsException try
catch
catch catch
catch Java
y 147 y
Java
CatchDemo.java:11: java.lang.ArithmeticException
catch(ArithmeticException e)
ArithmeticException
9.2.2
printStackTrace()
9
method1();
}
static void method1()
{
method2();
}
static void method2()
{
method3();
}
static void method3()
{
String str=null;
int n=str.length();
}
// method1
// method2
// method3
// null
//
java.lang.NullPointerException
at TestprintStackTraceDemo.method3(TestprintStackTraceDemo.java:23)
at TestprintStackTraceDemo.method2(TestprintStackTraceDemo.java:18)
at TestprintStackTraceDemo.method1(TestprintStackTraceDemo.java:14)
y 149 y
Java
at TestprintStackTraceDemo.main(TestprintStackTraceDemo.java:5)
try catch
e.printStackTrace();
NullPointerException Java
public class DBDemo {
public static void main(String[ ] args) {
Class.forName("");
}
}
DBDemo.java:4: java.lang.ClassNotFoundException
Class.forName("");
^
1
ClassNotFoundException
public class DBDemo {
public static void main(String[ ] args) {
try
{
Class.forName("");
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
java.lang.ClassNotFoundException:
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at DBDemo.main(DBDemo.java:5)
catch
9.2.3 finally
9
catch(NullPointerException e){
e.printStackTrace();
}
System.out.println("");
}
}
java.lang.NullPointerException
at SimpleDemo.main(SimpleDemo.java:6)
try Java
catch
Java finally
finally
try-catch finally
public class SimpleDemo {
public static void main(String[ ] args) {
String str=null;
int strLength=0;
try{
strLength=str.length();
System.out.println("");
}
catch(NullPointerException e){
e.printStackTrace();
}
finally
{
System.out.println(" finally ");
}
System.out.println("");
}
}
java.lang.NullPointerException
at SimpleDemo.main(SimpleDemo.java:6)
finally
finally catch
Java
//System.out.println("");
}
catch(NullPointerException e){
e.printStackTrace();
}
finally
{
System.out.println(" finally ");
}
System.out.println("");
}
}
finally
finally
9.2.4
1 throws
Java throws
public class ThrowsDemo {
static void method()throws NullPointerException,
IndexOutOfBoundsException,ClassNotFoundException{
String str=null;
int strLength=0;
strLength=str.length();
System.out.println(strLength);
}
public static void main(String[ ] args) {
try {
method();
} catch (NullPointerException e) {
System.out.println("NullPointerException ");
e.printStackTrace();
} catch (IndexOutOfBoundsException e) {
System.out.println("IndexOutOfBoundsException ");
e.printStackTrace();
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException ");
e.printStackTrace();
}
}
}
y 152 y
NullPointerException
java.lang.NullPointerException
at ThrowsDemo.method(ThrowsDemo.java:5)
at ThrowsDemo.main(ThrowsDemo.java:10)
() throws 1, 2{
//
}
throw
2 throw
throw
try-catch
ThrowsDemo 3
try-catch catch
try-catch
try-catch
3 throwsthrow
throws throws
throw
public class ThrowsDemo1{
y 153 y
Java
static int amethod(int a,int b) //throws ArithmeticException
{
if(b==0)
throw new ArithmeticException();
else
return a/b;
}
public static void main(String args[ ]){
System.out.println(amethod(15,5));
System.out.println(amethod(15,0));
}
}
amethod ArithmeticException
3
Exception in thread "main" java.lang.ArithmeticException
at ThrowsDemo1.amethod(ThrowsDemo1.java:5)
at ThrowsDemo1.main(ThrowsDemo1.java:11)
ThrowsDemo2.java:6: java.lang.IllegalAccessException
IllegalAccessException
try-catch
9
System.out.println("");
}
}
}
9.3
Java
9.3.1
Exception
throwable
Java
MyException:
9.3.2
9
System.out.println("120 "+level);
}
catch(IllegalScoreException e)
{
e.printStackTrace();
}
}
static String scoreLevel(int score) throws IllegalScoreException
{
if(score>=85&&score<=100)
return "A";
else if(score>=75&&score<85)
return "B";
else if(score>=60&&score<75)
return "C";
else if(score<60&&score>=0)
return "D";
else
throw new IllegalScoreException("");
}
}
IllegalScoreExceptionScore
try-catch
catch IllegalScoreException
90 A
IllegalScoreException:
at Score.scoreLevel(Score.java:38)
at Score.main(Score.java:19)
9.4
Java
Java Java
Java Java
y 157 y