This is a simple lab. Add in the minor changes as documented in the Lab 6 Files
ID: 3537908 • Letter: T
Question
This is a simple lab. Add in the minor changes as documented in the Lab 6 Files and study the handling of exceptions.
The %u201Cmain%u201D tryit.java does not need to be edited.
Your output when finished should appear as:
I threw this
In finally
/ by zero
keep on running
_____________________________________________________________________
package tryit;
/**
*
* @author jv
*/
public class Tryit {
/**
* @param args the command line arguments
*/
//
// NO changes are required to tryit.java (ie. This file)
public static void main(String[] args)
{
Trouble trouble = new Trouble();
if( trouble.thisReturnsTrue()) LogFile.lg( "This is a simple test of the log file. ");
if( trouble.unexpectedResult() ) LogFile.lg( "UnexpectedResult returned True ");
else LogFile.lg( "UnexpectedResult returned False");
try{ trouble.throwBecauseICan(); }
catch( Exception e ) { S.m(e.getMessage() ); }
try{ trouble.divideByZero(); }
catch( Exception e1 ){ S.m( e1.getMessage()); }
S.m("keep on running");
}
}
Explanation / Answer
package tryit;
/**
*
* @author jv
*/
public class Tryit {
/**
* @param args the command line arguments
*/
//
// NO changes are required to tryit.java (ie. This file)
public static void main(String[] args)
{
Trouble trouble = new Trouble();
new LogFile();
if( trouble.thisReturnsTrue())
LogFile.lg( "This is a simple test of the log file. ");
if( trouble.unexpectedResult() ) LogFile.lg( "UnexpectedResult returned True ");
else LogFile.lg( "UnexpectedResult returned False");
try{ trouble.throwBecauseICan(); }
catch( Exception e ) { S.m(e.getMessage() ); }
try{ trouble.divideByZero(); }
catch( Exception e1 ){ S.m( e1.getMessage()); }
S.m("keep on running");
}
}
--------------------------------------------------------------------
package tryit;
public class Trouble
{
Trouble(){}
public boolean thisReturnsTrue(){ return true; }
public boolean unexpectedResult(){
try { return false;}
finally { return true; }
}
public boolean throwBecauseICan ()throws Exception {
try
{ S.m("I threw this"); }
finally
{ LogFile.lg(" throwBecauseICan ");}
return false;
}
void divideByZero() {
try{
two();
}
finally
{ S.m("In finally");}
}
void two()
{ int j;
for( int i = 1; i < 7; i-- )
{ j = i/i;}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.