Previous Topic Next topic Print topic


Exception Handling - in COBOL and Java

Exception Handling in Managed COBOL

*> Throw an exception
declare myException as type Exception = 
         new Exception(“Something is really wrong.").
raise myException 
 
*> Catch an exception. 
try
  set y to 0;
  compute x = 10 / y
catch myException  *> Argument is optional, no "When" keyword 
  display myException::Message
end-try

Exception Handling in Java

import java.lang.*;
// Throw an exception
Exception up = new Exception("Something is really wrong.");
throw new EmptyStackException();  
	 
// Catch an exception
try 
{ 
  int y = 0;
  int x = 10 / y;
}
catch (ArithmeticException ex) // Argument optional, no "When" keyword 
{
  System.out.println(ex.getMessage());
}
finally 
{
  System.out.println("\007");
} 	

Portions of these examples were produced by Dr. Frank McCown, Harding University Computer Science Dept, and are licensed under a Creative Commons License.

Previous Topic Next topic Print topic