Java Programming class – Department of Network

advertisement

Java Programming 3 rd

class – Department of Network

Mehdi Ebady Manaa College of IT- University of Babylon

1.

Exception

Exception are a mechanism used by many programming language to describe what to do when something unexpected happens. Typically, something unexpected is an error of some sort, for example when a method is invoked with unacceptable arguments, or a network connections fails , or the user ask to open a non-existed file.

The java programming language provides two broad categories of exceptions , known as checked and unchecked exceptions

Checked Exception are those that programmer is expected to handle in the program.

These are represented by the Exception class.

Example

File not being found or a network failure

Unchecked exception might arise from the conditions that represent bugs, or situations that are considered generally too difficult for a program to handle reasonably.

Example

Attempt to access beyond the end of an array

Errors are exception that arises from environmental issues that are rare enough or hard enough to recover from.

Example

Fatal situations are represented by the Error class. Probable bugs are represented by the RuntimeException class.

Page 1 Date: Tuesday, January 07, 2014

Java Programming 3 rd

class – Department of Network

Mehdi Ebady Manaa College of IT- University of Babylon

Page 2

Figure 1: Subclasses and Exceptions

Exception-Code Example public class AddArguments { public static void main(String args[]) { int sum = 0; for ( String arg : args ) {

sum += Integer.

parseInt (arg);

}

System.

out .println( "Sum = " + sum);

}

Sum = 10 for args [] ={1,2,3,4}

If we pass args [] ={1,two,3.0,4}

Exception in thread "main" java.lang.NumberFormatException: For input string: "two" at java.lang.NumberFormatException.forInputString(NumberFormatEx ception.java:48) at java.lang.Integer.parseInt(Integer.java:447)

Date: Tuesday, January 07, 2014

Java Programming 3 rd

class – Department of Network

Mehdi Ebady Manaa College of IT- University of Babylon at java.lang.Integer.parseInt(Integer.java:497) at AddArguments.main(AddArguments.java:5)

2.

The try-catch Statement

The java programming language provides a mechanism for figuring out which exception was thrown and how to recover from it by using try-catch exception public class AddArguments2 { public static void main(String args[]) { try { int sum = 0; for ( String arg : args ) { sum += Integer.

parseInt (arg);

}

System.

out .println( "Sum = " + sum);

} catch (NumberFormatException nfe) {

System.

err .println( "One of the command-line "

+ "arguments is not an integer." );

}

}

}

If we call the method with

1 two 3.0 4 for args

Result

One of the command-line arguments is not an integer.

3.

Fine-grained Exception Handling

The try-catch statement can be used on smaller chunks of code. public class AddArguments3 { public static void main(String args[]) { int sum = 0; for ( String arg : args ) { try { sum += Integer.parseInt(arg);

} catch (NumberFormatException nfe) {

System.err.println("[" + arg + "] is not an integer"

+ " and will not be included in the sum.");

}

}

System.out.println("Sum = " + sum);

}

Page 3 Date: Tuesday, January 07, 2014

Java Programming 3 rd

class – Department of Network

Mehdi Ebady Manaa College of IT- University of Babylon

}

If we call the method with

1 two 3.0 4 for args

Results

[two] is not an integer and will not be included in the sum.

[3.0] is not an integer and will not be included in the sum.

Sum = 5

4.

Try-catch – finally

The finally clause defines a block of code that always executes. try { startFaucet(); waterLawn();

} catch (BrokenPipeException e) { logProblem(e);

} finally { stopFaucet();

}

NullPointerException

FileNotFoundException

NumberFormatException

ArithmeticException

SecurityException

E xamples of common Exceptions

The Handle or Declare Rule

Use the handle or declare rule as follows: Handle the exception by using the try-catch-finally block. OR Declare that the code causes an exception by using the throws clause. void trouble() throws IOException { ... } void trouble() throws IOException, MyException { ... }

Other Principles

You do not need to declare runtime exceptions or errors.

You can choose to handle runtime exceptions.

Page 4 Date: Tuesday, January 07, 2014

Download