answersLogoWhite

0


Best Answer

An exception is literally that: an exception. Exceptions are thrown when something that shouldn't happen does. For example, if you're trying to turn "Hi" into an Integer, you'll get a NumberFormatException (in Java.) If there is nothing in place to handle an exception, the program will crash. Try-catch blocks are used for exception handling within methods, and exception throwing is used to give out exceptions, or let a different method handle it.

Example program (Java):

class ex{

public static void main(String[] args){

String Strx = "5a";

int x = 0;

try{

x += Integer.parseInt(Strx);

}catch(NumberFormatException e){ // If Strx is not an Integer

x += Integer.parseInt("5");

}

System.out.println("The value of x is " + x); // The value of x is 5

}

}

Alternatively, you could remove the try-catch blocks, and simply write "public static void main(String[] args) throws NumberFormatException" in the 2nd line of the program.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is an exception With an example program explain how it is handled?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

A program to explain the Exception Handling mechanisms in Java using the keywords try catch and finally?

Here is a code snippet illustrating exception handling: try { int a= 3 / 0 ; } catch ( ArithmeticException e ) { System.out.println ("An ArithmeticException has occured"); } finally { // some code }


What is difference between exception and error in java and explain using a program?

Error occurs at runtime and cannot be recovered, Outofmemory is one such example. Exceptions on the other hand are due conditions which the application encounters, that can be recovered such as FileNotFound exception or IO exceptions


What happens when raised exception is not caught by catch block?

A non-caught exception is propagated out of the local catch block into the next catch block, daisy chaining to the outermost catch block in the run-time library, where it will be handled by abending the program.


Explain the following tearm in the context of object oriented programming Also explain how these concept are implement in c by given an example program for each?

Explain the following terms in the context of object oriented programming. Also explain how these concepts are implemented in C++ by giving an example program for each.


What is catch net?

When an exception occurs in program execution. Such as 1/0 or divide by zero exception. The program must catch the exception or the program will crash. Although handling of the exception or solution is not necessary.


Can exception handling resolve the abnormal flow of a program?

An exception is abnormal program flow, so handling the exception is the very definition of resolving the abnormality.


What is persistence in oops explain with example?

The phenomenon where the object outlives the program execution time & exists between execution of a program is known as persistance


What is checked exception?

A compiler for the Java programming language checks, at compile time, that a program contains handlers for checked exceptions, by analyzing which checked exceptions can result from execution of a method or constructor. For each checked exception which is a possible result, the throws clause for the method or constructor must mention the class of that exception or one of the superclasses of the class of that exception. This compile-time checking for the presence of exception handlers is designed to reduce the number of exceptions which are not properly handled.


How exception handling mechanism can be used for debugging a program in java?

Trapping and handling of runtime errors is one of the most crucial taska ahead of any programmer. AS A DEVELOPER, YOU SOMETIMES SEEM TO SPEND MORE TIME CHECKING FOR ERRORS AND HANDLING THEM THEN YOU DO ON THE CORE LOGIC OF THE ACTUAL PROGRAM.


Why handle exception?

Because you can and you should, unless you want your program to crash if an exception occurs.


Explain the design and application of arrays and how an array simplifies program development?

I need an example of a real-world array


IS Throwing an exception always causes program termination?

No Not necessarily.. You can catch an exception and then continue to execute your program as if nothing happened. If in your catch block you have code like System.exit then your program would terminate.