10.30.17
Exceptions
Problems in java code that interrupt normal execution
Two types
1) Errors
~~
~~~
~~~
2)Exceptions - can be "handled".
runtimeexcepion
ioexcepion
securityexception
Throwable - subclass of error
Can't do much w/ runtime error
code has to check exception
no wide net to catch all
everything i can think of that can go wrong
has to
two types
Checked
Unchecked
just incase error happens, runtimeexception
Exception Classes
Where can it happen?
Main(Exception can be here)
Call
Call
Calc
Calc (Best place to put exception here)
Parseint
Examples
if(parameter val is not an int)
throw new NumberFormatException() -- particular
-
System Stack
go bottums up
go down w/ code
exceptions stop code & restart or circumvent restart process
stop & sends to previous point (not w/o code o handle it)
try
{
value = Integer.parseInt(str);
myArray[row] = value
}
catch(NumberFomatException nfe) - first underneath is most specific
{
print("Enter Integer")
}
catch()
{
print("Index out..")
}
catch()
{
print
}
catch(Exception e) - anything else
{
print
}
Enclose code that might generate an error.
try must be followed by one or more catch.
Be diplomatic where I put things.
Catch it where Im doing the stuff. Avoid two seperate catch blocks. I want 1.
try
catch (potenial eror in r)
catch (potential error in z)
public void x
call y
public void y
call z
public void z
point of exceptions is to not stop code
a default(no param)
excepion_objec_idenifier.Sring //-- print out exception name
pass in()
enter string IE "Enter INT"
throws NumberFormatException
public char charAt(int i)
{
if (i more than this.length)
throw new StringIndexOutOBoundsException();
else
return
}
refocus brings back to where exception occured
numfield.requestFocus() (inside try)
I can make my own throws exception as "throws" && making new class of with matchin method calling super
public float quotient(int num, int denom) throws AttemptedDivideByZeroException
{
if (denom == 0)
thow new AttemptedDivideByZeroException();
return (float)num/denom;
}
///extends Exception
class AttemptedDivideByZeroException extends Exception //making our own exception
public AttemptedDivideByZeroException()
{
super("zero divide error");
}
try with a loop is not as good as a loop with a try
try
{
while(hasmoreTokens
oken =
i =
}
if indexof(".") != -1
throw new InorrectValueExcepion(); //acts like a return
//when it's not right
//rest of formula here without else for IncorrectValue
//try looks at 1 line. If exception then breaks out of while && move on. Process quicker
//throw new IncorrecValueException("Qualit mus be int") - alternative
while // not something i can stop unlike the try while above
{
try
{
}
catch (IncorrectValueException ivf)
{
}
catch (ArrayIndexOutOfBoundsException aoe)
{
}
catch
{
}
}
//While loop doesn't stop once one is returned. Problem if looking for 5 we may have 4
//One overwrites the next
//Try location is critical. Think about where the problem can be.
Third way- Propagating Exceptions - both inside && outside while
Inside method
try{ while( call method with bottum try ){} }
catch() --- caught as bottum try. Catches here and throws at the bottum try. Pauses at this point if bottum try is catched. Can't do anything if catched. Until it handles exception. Can be stopped && refocused. Depends when i'm returning
if(){}
inside method
try{}// --- this is being thrown. Throws back into while loop
catch{} //stops if called. Completely different thrown than other try catch
catch{} //stops if called. Completely different thrown than other try catch
Seperate exceptions thrown in seperate locations