Multiple catch Blocks Assignment Help

Assignment Help: >> Exception Types - Multiple catch Blocks

Multiple catch Blocks:

Within  a few  cases,  a  method might  have  to  catch  various kinds  of  exceptions. Java supports multiple catch blocks. Every catch block must specify a various type of exception:

try

{

// method calls go here

}

catch( SomeExceptionClass e )

{

// handle exceptions here

}

catch( SomeOtherExceptionClass e )

{

// handle exceptions here

}

While an exception is thrown in the try block, this is caught through the first catch block of the appropriate type. Only one catch block in a provided set will be executed. Note that the catch block looks a lot such as a method declaration. The exception caught in a catch block is a local reference to the real exception object. You could use this exception object to help determine what caused the exception to be thrown within the first place.

A method which ignores exceptions thrown through the method it calls.

import Java.io.* ;

import Java.lang.Exception ;

public class MultiThrow {

public static void main( String[] args ) {

try

{

foo() ;

}

catch( Exception e )

{

System.out.println( "Caught exception " +

e.getMessage() ) ;

}

}

static void foo() throws Exception {

bar() ;

}

static void bar() throws Exception {

throw new Exception( "Who cares" ) ;

}

}

Within the example main () calls foo() that calls bar(). Since bar () throws an exception and doesn't catch it, foo() has the opportunity to catch it. A foo() method has no catch block that's why it cannot catch the exception. Within case, the exception propagates up the call stack to foo()'s caller and main().

// A method which catches and re throws an exception.

import java.io.* ;

import java.lang.Exception ;

public class MultiThrow {

public static void main( String[] args ) {

try

{

foo() ;

}

catch( Exception e )

{

System.out.println( "Caught exception " +

e.getMessage() ) ;

}

}

static void foo() throws Exception {

try

{

bar() ;

}

catch( Exception e )

{

System.out.println( "Re throw exception -- " +

e.getMessage() ) ;

throw e ;

}          

}

static void bar() throws Exception {

throw new Exception( "Who cares" ) ;

}

}

In the instance main() calls foo() that calls bar(). Since bar() throws an exception and doesn't catch it, foo() has the opportunity to catch it. A foo() method has no catch block, so it cannot catch the exception. Within this case, the exception propagates up the call stack to foo()'s caller that is main().

The foo() function calls bar(). The bar () function throws an exception and foo() catches it. In this instance, foo() simply rethrows the exception, that is ultimately caught in the application's main() function. Within a real application, foo() could do a few processing and then rethrow the exception. This arrangement permits foo() and main()both to handle the exception.

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