Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

javappt and for programming knowledge, Thesis of Java Programming

study well and get java programming knowledge to get success in life

Typology: Thesis

2017/2018

Uploaded on 10/02/2018

hemavathi-j
hemavathi-j 🇮🇳

5

(3)

11 documents

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Souradeep Saha
Writayan Das
Exception Handling
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download javappt and for programming knowledge and more Thesis Java Programming in PDF only on Docsity!

Souradeep Saha

Writayan Das

Exception Handling

Introduction

What is an Exception?

An exception is an unexpected event that occurs during

runtime and causes normal program flow to be disrupted.

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

  • Write code such that it raises an error flag every time something goes wrong
  • [throw exception]

2

  • Error flag is raised, then

3

  • Call the Error-handling routine
  • [catch exception]

Concept of Exception Handling

Exception An unexpected error that^ occurs during runtime

Throwing

The process by which an exception is generated and passed to the program

Catching

Capturing an exception that has just occurred and executing statements that try to resolve the problem

Catch

clause or

catch

block

The block of code that attempts to deal with the exception

Stack

trace

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 exception ;

— 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 using namespace std; void Xtest(int test) { cout << "Inside Xtest, test is: " << test << "\n"; if(test) throw test; } int main() { cout << "Start\n"; try { cout << "Inside try block\n"; Xtest(0); Xtest(1); Xtest(2); } catch (int i) { cout <<"Caught an exception -- value is: "; cout << i << "\n"; } cout << "End"; return 0; }

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 #include using namespace std;

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

}