0% found this document useful (0 votes)
50 views

Java Exception

This document discusses exception handling in Java programs. It explains that exceptions are used to handle errors that occur during program execution. Code that may generate errors is placed in a try block, and catch blocks handle specific exceptions. When an error occurs, an exception object is generated containing error information. The example code catches a NumberFormatException if non-numeric input is entered for integer variables, printing an error message.

Uploaded by

Gaurav Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Java Exception

This document discusses exception handling in Java programs. It explains that exceptions are used to handle errors that occur during program execution. Code that may generate errors is placed in a try block, and catch blocks handle specific exceptions. When an error occurs, an exception object is generated containing error information. The example code catches a NumberFormatException if non-numeric input is entered for integer variables, printing an error message.

Uploaded by

Gaurav Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Java Exception - Exception Handling in Java

Exception, that means exceptional errors. Actually exceptions are used for handling errors in programs that occurs during the program execution. During the program execution if any error occurs and you want to print your own message or the system message about the error then you write the part of the program which generate the error in the try{} block and catch the errors using catch() block. Exception turns the direction of normal flow of the program control and send to the related catch() block. Error that occurs during the program execution generate a specific object which has the information about the errors occurred in the program. In the following example code you will see that how the exception handling can be done in java program. This example reads two integer numbers for the variables a and b. If you enter any other character except number ( 0 - 9 ) then the error is caught by NumberFormatException object. After thatex.getMessage() prints the information about the error occurring causes. Code of the program :

import java.io.*; public class exceptionHandle{ public static void main(String[] args) throws Exception{ try{ int a,b; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); a = Integer.parseInt(in.readLine()); b = Integer.parseInt(in.readLine()); } catch(NumberFormatException ex){ System.out.println(ex.getMessage() + " is not a numeric value."); System.exit(0); } } }

You might also like