Using try and catch Assignment Help

Assignment Help: >> Exception Types - Using try and catch

Using try and catch:

Although the default exception handler given through the Java run-time system is useful for debugging, you will commonly need to handle an exception yourself.  Doing so gives two advantages.  First, it permits you to fix the error. Second, it avoids the program from automatically terminating.  Many users would be confused if your program stopped running and printed a stack trace every time when an error occurred! Fortunately it is quite simple to avoid this.

To guard against and handle a run-time error, simply enclose the code which you need to monitor inside a try block.  Instantly following the try block, involves a catch cluse which specifies the exception type which you wish to catch. To describe how simply this can be complete, the following program involves a try block and a catch clause that processes the ArithmeticException produced through the division-by-zero error.

class Exc2 {

public static void main (String args[]) {

int d,a;

try { // monitor a block of code.

d=0;

a=42/d;

system.out.println("This will not be printed.");

}  catch (ArithmeticException e) { // catch divide-by-zero error

System.out.println("Division by zero.");

}

System.out.println("After catch statement");

}

This program produces the output:

Division by Zero

After catch statement.

Remember that the call println ( ) inside the try block is never executed.  At one time an exception is thrown; program control transfers out of the try block and came into the catch block.

Put another way, catch is not "called" then execution never "returns" to the try block from a catch.  "Therefore, the line this will not be printed" is not shown. At one time the catch statements has executed, program control continues along with the further line in the program given the whole try/catch mechanism.

A try and its catch statement form a unit.  A scope for the catch clause is restricted to that statements specified through the immediately preceding try statement. A catch statement cannot catch an exception by through another try statement (except in the case of nested try statements, described shortly.)  The statements that are protected through try must be surrounded by curly braces. (That is they must be inside a block). We cannot use try on a single statement.

The target of most well constructed catch clauses should be to resolve the exceptional condition and then continue on as if the error has never happened. For instance, in the next program every interaction of for loop gains two random integers. Those two integers are separated through each other, and the result is used to divide the value 12345.   The   final result is put into a. it is caught, a value of a is set to be zero if either division operation causes a divide by zero error or the program continues.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd