Exceptional Example Assignment Help

Assignment Help: >> Exception Types - Exceptional Example

Exceptional Example:

Since all Java functions are class members, the passingGrade() method is incorporated in the gradeTest application class. Since main() calls passingGrade(), main() must be able to catch any exceptions passingGrade() may throw. To do this, main () places the call to passingGrade() in a try block. Since the throws clause lists type Exception, A catch block catches the Exception class.

//The gradeTest application.

import Java.io.* ;

import Java.lang.Exception ;

public class gradeTest {

public static void main( String[] args ) {

try

{

// passingGrade throws a second call to

// an exception then the third call never

// gets executed

System.out.println( passingGrade( 75, 80 ) ) ;

System.out.println( passingGrade( 85,  0 ) ) ;

System.out.println( passingGrade( 80, 95 ) ) ;

}

catch( Exception e )

{

System.out.println( "Caught exception --" +

e.getMessage() ) ;

}

}

static boolean passingGrade( int correct, int total )

throws Exception {

boolean returnCode = false ;

if( correct > total ) {

throw new Exception( " Invalid" ) ;

}

if ( (float)correct / (float)total > 0.70 ) {

returnCode = true ;

}

return returnCode ;

}

}

The second call to passingGrade() fails in this case, since the method checks to see whether the number of correct responses is less than the total responses. Whenever passingGrade() throws the exception, control passes to the main() method. Within this instance, the catch block in main () catches the exception and prints Caught exception -- Invalid.

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