Java run-time exceptions In Java, a run-time exception is one that does not have to be caught by the program. The compiler will not report an error if the programmer does not handle a run-time exception. An example of a run-time exception is ArithmeticException . This exception occurs when the program tries to carry out an arithmetic operation which cannot succeed (like dividing by zero). The programmer does not have to handle this exception. What happens if the exception is thrown and the programmer does not handle it? The program will automatically be stopped, and the name of the exception displayed. This is a very ugly way for a program to finish, so the programmer may choose to handle the exception anyway. Handling a run-time exception is no different from handling a normal exception. Here is an example of the use of ArithmeticException : { int x = 2/0; } catch (ArithmeticException) { // Display a helpful error message here }
Run-time exceptions differ from ordinary exceptions in that they are subclasses of RuntimeException , rather than of Exception . Back to top 
RITSEC - Global Campus Copyright ?1999 RITSEC- Middlesex University. All rights reserved. webmaster@globalcampus.com.eg |