






















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
study well and get java programming knowledge to get success in life
Typology: Thesis
1 / 30
This page cannot be seen from the preview
Don't miss anything!
Introduction
What is an Exception?
Some common examples:
Divide by zero errors Accessing the elements of an array beyond its range Invalid input Hard disk crash Opening a non-existent file Heap memory exhaustion
1
2
3
Concept of Exception Handling
Exception An unexpected error that^ occurs during runtime
The process by which an exception is generated and passed to the program
Capturing an exception that has just occurred and executing statements that try to resolve the problem
The block of code that attempts to deal with the exception
The sequence of method calls that brought control to the point where the exception occurred
Terminology
If the method contains code that may cause a checked exception, we MUST handle the exception OR pass the exception to the parent class (every class has Object as the ultimate parent)
₪ To handle the exception, we write a “try-catch” block.
₪ To pass the exception “up the chain”, we declare a throws clause in our method or class declaration.
How do we handle exceptions?
import java.io.*; public class ExcepTest { public static void main(String args[]) { try { int a[] = new int[2]; System.out.println("Access element three :" + a[3]); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Exception thrown :" + e); } System.out.println("Out of the block"); } }
Java: Try-Catch Mechanism
Wherever the code may trigger an exception, the normal code logic is placed inside a block of
code starting with the “try”
keyword:
After the try block, the code to handle the exception should it arise is placed in a block of code starting with the
“catch” keyword.
C++: General form of try and catch
try { // try block } catch (type1 arg) { // catch block } catch (type2 arg) { // catch block } catch (type3 arg) { // catch block } ... catch (typeN arg) { // catch block }
Note: The try can be as short as a few statements within one function or as all - encompassing as enclosing the main() function code within a try block.
The throw keyword
Note:
› If an exception is thrown for which there is no applicable catch statement, an abnormal program termination may occur.
› Throwing an unhandled exception causes the standard library function terminate() to be invoked.
› By default, terminate() calls abort() to stop the program, but we can specify our own termination handler.
General Form:
— throw generates the exception specified by exception — throw must be executed within a try block — throw can also be executed within a function called from a try block
Java Exception Hierarchy
Java: Creating Exceptions
class WordContainsException extends Exception { //Parameterless Constructor public WordContainsException(){} //Constructor that accepts a message public WordContainsException(String message) { super(message); } }
try { if(word.contains(" ")) { throw new WordContainsException(); } } catch(WordContainsException ex) { //Process message however you would like }
Usage:
#include
Output:
Start Inside try block Inside Xtest, test is: 0 Inside Xtest, test is: 1 Caught an exception -- value is: 1 End
C++: Throwing an exception from outside try block
Catching Class Types: An Example
#include
class MyException { public: char str_what[80]; int what; MyException() { *str_what = 0; what =0; } MyException(char *s, int e) { strcpy(str_what, s); what = e; } };
int main() { int i; try { cout << "Enter a positive number: "; cin >> i; if(i<0) throw MyException("Not Positive", i); } catch (MyException e) { cout << e.str_what << ": "; cout << e.what << "\n"; } return 0; }
Sample run:
Enter a positive number: - Not Positive: -
Exception Handling Options
There are several additional features and nuances to C++ exception handling that make it easier and more convenient to use. These attributes are discussed here.
Catching All Exceptions
In some circumstances we want an exception
handler to catch all exceptions instead of just a
certain type.
This is easily accomplished by using this form of catch:
catch(...)
{ // process all exceptions
}