I. Java Exception

Java Exception is a problem when executing a code flow and they can stop the execution if not handled carefully. There are 2 kinds of the Exception: CheckedException & UnCheckedException.

1. Java CheckedException

CheckedException is an exception that can be detected by Java compiler. Editors can help developers to detect by highlight them at development time. CheckedException extends java.lang.Exception, some common CheckedException: FileNotFoundException, IOException …

With CheckedException, developers must handle it by try/catch statement or throws keyword.

Use try/catch:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

try{

StringFILE_NAME="C:\\test\\test.txt";

FileReader fr=newFileReader(newFile(FILE_NAME));

char[]a=newchar[50];

fr.read(a);// reads the content to the array

for(charc:a)

System.out.print(c);// prints the characters one by one

fr.close();

}catch(FileNotFoundExceptione){

// do some thing

System.out.println(e.getMessage());

}catch(IOExceptione){

// do some thing like:

System.out.println(e.getMessage());

}

or use throws keyword:

1

2

3

4

5

6

7

8

9

publicstaticvoidmain(String[]args)throwsIOException{

StringFILE_NAME="C:\\test\\test.txt";

FileReader fr=newFileReader(newFile(FILE_NAME));

char[]a=newchar[50];

fr.read(a);// reads the content to the array

for(charc:a)

System.out.print(c);// prints the characters one by one

fr.close();

}

2. Java UncheckedException

UncheckedException is a kind of Exception that occurs at the executing time, so it can be called: RunTime Excepion. UncheckedException does not be detected by compiler because they are related with logic code at runtime.UncheckedException extends from java.lang.RuntimeException, some common UncheckedException like: NullPointerException, IndexOutOfBoundsException, IllegalArgumentException, ArithmeticException.

Sample java.lang.ArithmeticException: / by zero

1

2

3

4

5

6

7

8

9

10

11

publicstaticvoidmain(String[]args){

System.out.println("# Code for Test: Unchecked Exception");

inti=1;

intk=2;

// do something

// ...

// then k = 0;

k=0;

intm=i/k;

System.out.println(m);

}

Exception when executing:

1

2

3

# Code for Test: Unchecked Exception

Exception in thread "main" java.lang.ArithmeticException: / by zero

at com.javasampleapproach.exception.JavaUnCheckedException.main(JavaUnCheckedException.java:12)

Runtime Exceptions are big problems so Developers need to handle it carefully by try/catch statement or throws/throw keyword.

II. Effect of Exceptions to Program Execution

With CheckedException are easy to detected and handled by helping of Java Compiler and Editors. But UncheckedExceptions are very risk, it can stop Program. So Developers need to understand the impact of it in deeply way.

1. Java RunTime Exception in Single Thread Program

Once again with sample java.lang.ArithmeticException: / by zero

1

2

3

4

5

6

7

8

9

10

11

publicstaticvoidmain(String[]args){

System.out.println("# Code for Test: Unchecked Exception");

inti=1;

intk=2;

// do something

// ...

// then k = 0;

k=0;

intm=i/k;

System.out.println(m);

}

The Exception is happened at int m = i / k;, and the program will be terminated right after it. try/catch statement will help program to handle the exception then continue processing remain codes.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

System.out.println("# Code for Test: Unchecked Exception");

inti=1;

intk=2;

intm=0;

try{

// do something

// ...

// then k = 0;

k=0;

m=i/k;

System.out.println(m);

}catch(Exceptione){

// do something such as logs it for investigating bugs

// then continue process the execution or throw e like it

log.info(e.getMessage());

m=-1;

}

System.out.println(m);

The exception is happened at point: m = i / k;, but The Program uses try/catch to handle it so the program continues processing with code segment: System.out.println(m);.

2. Java RunTime Exception in Multi-Thread Program

Java Exception only affects to a current Thread (the executing flow that causes exception).
Let go with below program has 2 child threads inside, both of them make exception but just only once handling exception:

grokonez

ABOUT US
We are passionate engineers in software development by Java Technology & Spring Framework. We believe that creating little good thing with specific orientation everyday can make great influence on the world someday.