Exception Handling

try-thow-catch

try
{
  CodeThatMayThrowAnException
}
throw new 
  ExceptionClassName(PossiblySomeArguments);

catch(Exception e)
{
  ExceptionHandlingCode
}

import java.util.InputMismatchException;

import java.io.IOException;

Exception Classes from Standard Packages

  • Every exception class is a descendent class of the class Exception
  • Although the Exception class can be used directly in a class or program, it is most often used to define a derived class
  • The class Exception is in the java.lang package, and so requires no import statement

Finally

  • The finally block contains code to be executed whether or not an exception is thrown in a try block
If it is used, a finally block is placed after a try block and its following catch blocks
try
{  . . .  }
catch(ExceptionClass1 e)
{  . . .  }
  . . . 
catch(ExceptionClassN e)
{  . . .  }
finally
{
  CodeToBeExecutedInAllCases
}

New text