CS-2301 Lab (9) 422
CS-2301 Lab (9) 422
هـ1442 الفصل الدراسى الثاني للعام الجامعي جامعة األمير سطام بن عبد العزيز
CS-2301 :المقرر كلية اآلداب والعلـوم بوادي الدواسر
78: الشعبة الرابع:المستوي قســـم علوم الحاسب
Lab-9
3-
do
{
try // read two numbers and calculate quotient
{
System.out.print( "Please enter an integer numerator: " );
int numerator = scanner.nextInt();
System.out.print( "Please enter an integer denominator: " );
int denominator = scanner.nextInt();
int result = quotient( numerator, denominator );
System.out.printf( "\nResult: %d / %d = %d\n", numerator,
denominator, result );
continueLoop = false; // input successful; end looping
} // end try
catch ( InputMismatchException inputMismatchException )
{
System.err.printf( "\nException: %s\n",
inputMismatchException );
scanner.nextLine(); // discard input so user can try again
System.out.println(
"You must enter integers. Please try again.\n" );
} // end catch
catch ( ArithmeticException arithmeticException )
{
System.err.printf( "\nException: %s\n", arithmeticException );
System.out.println(
2
Mr. Asad Abd Elrashid
"Zero is an invalid denominator. Please try again.\n" );
} // end catch
} while ( continueLoop ); // end do...while
} // end main
} // end class DivideByZeroWithExceptionHandling
4-
doesNotThrowException();
} // end main
// demonstrate try...catch...finally
public static void throwException() throws Exception
{
try // throw an exception and immediately catch it
{
System.out.println( "Method throwException" );
throw new Exception(); // generate exception
} // end try
catch ( Exception exception ) // catch exception thrown in try
{
System.err.println(
"Exception handled in method throwException" );
throw exception; // rethrow for further processing
} // end catch
finally // executes regardless of what occurs in try...catch
{
System.err.println( "Finally executed in throwException" );
} // end finally
// any code here would not be reached
Task:
2-
// Uses assert to check that an absolute value is positive
import java.util.Scanner;
public class AssertTest
{
public static void main( String args[] )
{
Scanner input = new Scanner( System.in );
System.out.print( "Enter a number between 0 and 10: " );
int number = input.nextInt();
// assert that the absolute value is >= 0
assert ( number >= 0 && number <= 10 ) : "bad number: " + number;
System.out.printf( "You entered %d\n", number );
} // end main
} // end class AssertTest +
3-
Find output of the following code:
// Demonstrating getMessage and printStackTrace from class Exception.
public class UsingExceptions
{
public static void main( String args[] )
{
try
{
method1(); // call method1
} // end try
catch ( Exception exception ) // catch exception thrown in method1
{
System.err.printf( "%s\n\n", exception.getMessage() );
exception.printStackTrace(); // print exception stack trace
// obtain the stack-trace information
StackTraceElement[] traceElements = exception.getStackTrace();
6
Mr. Asad Abd Elrashid
// call method3; throw exceptions back to method1
public static void method2() throws Exception
{
method3();
} // end method method2
8
Mr. Asad Abd Elrashid