SlideShare une entreprise Scribd logo
1  sur  9
Rethrowing Exception and User
defined exception
 Rajan Shah
Name:
Exception
 Exception are of two types:
 Synchronous exceptions- Errors such as “out-
of-range index” and “overflow”.
 Asynchronous exceptions- Errors that are
caused by the events beyond the control of the
program.
 Purpose- to provide means to detect and
report an “exceptional circumstance” so that
appropriate action can be taken.
Error handling code
 The error handling code performs the following
tasks:
1. Find the problem (Hit the exception),
2. Inform that an error has occurred (Throw the
exception),
3. Receive the error info (Catch the exception)
and
4. Take corrective action (Handle the
exception).
Exception handling mechanism
 Try: The keyword try is used to preface a block
of statements which may generate exceptions.
 Throw: Exception is thrown using throw
statement in try block.
 Catch: Catches the exception thrown by the
throw statement in the try block.
Rethrowing an Exception
 Rethrowing an expression from within an
exception handler can be done by calling
throw, by itself, with no exception (without
arguments).
 This causes the current exception to be
passed on to an outer try/catch sequence.
 An exception can only be rethrown from within
a catch block.
 When an exception is rethrown, it is
propagated outward to the next catch block.
Rethrowing an Exception
 #include <iostream>
using namespace std;
void MyHandler()
{
try
{
throw “hello”;
}
catch (const char*)
{
cout <<”Caught
exception inside
MyHandlern”;
throw; //rethrow char* out
of function
}
}
 int main()
{
cout<< “Main start”;
try
{
MyHandler();
}
catch(const char*)
{
cout <<”Caught
exception inside Mainn”;
}
cout << “Main end”;
return 0;
}
User Defined Exception
 #include <iostream>
#include <exception>
using namespace std;
struct MyException : public
exception
{
const char * what () const
throw ()
{
return "C++ Exception";
}
};
int main()
{
try
{ throw MyException(); }
catch(MyException& e)
{
std::cout << "MyException
caught" << std::endl; std::cout
<< e.what() << std::endl;
}
}
User Defined Exception
 The example shows the use of std::exception
class to implement user defined exception.
 what() -- is a public method provided by
exception class and it has been overridden by
all the child exception classes. This returns the
cause of an exception.
Rethrowing exception- JAVA

Contenu connexe

Tendances

file handling c++
file handling c++file handling c++
file handling c++
Guddu Spy
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
kamal kotecha
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
Suneel Dogra
 

Tendances (20)

Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Strings in C
Strings in CStrings in C
Strings in C
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Input output streams
Input output streamsInput output streams
Input output streams
 
array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++
 
file handling c++
file handling c++file handling c++
file handling c++
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
 
String in java
String in javaString in java
String in java
 
Java IO Package and Streams
Java IO Package and StreamsJava IO Package and Streams
Java IO Package and Streams
 
File Handling In C++
File Handling In C++File Handling In C++
File Handling In C++
 
C++ Memory Management
C++ Memory ManagementC++ Memory Management
C++ Memory Management
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
 
Exception Handling in C++
Exception Handling in C++Exception Handling in C++
Exception Handling in C++
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++
 
Exception handling
Exception handlingException handling
Exception handling
 
Stack Operation In Data Structure
Stack Operation In Data Structure Stack Operation In Data Structure
Stack Operation In Data Structure
 
Exception handling
Exception handlingException handling
Exception handling
 

Similaire à Rethrowing exception- JAVA

Md07 exceptions&assertion
Md07 exceptions&assertionMd07 exceptions&assertion
Md07 exceptions&assertion
Rakesh Madugula
 
Exceptions ref
Exceptions refExceptions ref
Exceptions ref
. .
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templates
farhan amjad
 
Exceptions &amp; Its Handling
Exceptions &amp; Its HandlingExceptions &amp; Its Handling
Exceptions &amp; Its Handling
Bharat17485
 
Unit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentUnit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application development
rohitgudasi18
 

Similaire à Rethrowing exception- JAVA (20)

Exception Handling
Exception HandlingException Handling
Exception Handling
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptions
 
Md07 exceptions&assertion
Md07 exceptions&assertionMd07 exceptions&assertion
Md07 exceptions&assertion
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Exceptions and Exception Handling in C++
Exceptions and Exception Handling in C++Exceptions and Exception Handling in C++
Exceptions and Exception Handling in C++
 
Exceptions ref
Exceptions refExceptions ref
Exceptions ref
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templates
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threads
 
Exceptions &amp; Its Handling
Exceptions &amp; Its HandlingExceptions &amp; Its Handling
Exceptions &amp; Its Handling
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Unit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentUnit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application development
 
Java -Exception handlingunit-iv
Java -Exception handlingunit-ivJava -Exception handlingunit-iv
Java -Exception handlingunit-iv
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
UNIT 2.pptx
UNIT 2.pptxUNIT 2.pptx
UNIT 2.pptx
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 

Plus de Rajan Shah (10)

Xml dtd- Document Type Definition- Web Technology
Xml dtd- Document Type Definition- Web TechnologyXml dtd- Document Type Definition- Web Technology
Xml dtd- Document Type Definition- Web Technology
 
Timing and control circuit
Timing and control circuitTiming and control circuit
Timing and control circuit
 
Np Completeness
Np CompletenessNp Completeness
Np Completeness
 
Lex Tool
Lex ToolLex Tool
Lex Tool
 
Files and streams In Java
Files and streams In JavaFiles and streams In Java
Files and streams In Java
 
Deadlock- Operating System
Deadlock- Operating SystemDeadlock- Operating System
Deadlock- Operating System
 
Data Reduction
Data ReductionData Reduction
Data Reduction
 
Cyclic Redundancy Check
Cyclic Redundancy CheckCyclic Redundancy Check
Cyclic Redundancy Check
 
Client server s/w Engineering
Client server s/w EngineeringClient server s/w Engineering
Client server s/w Engineering
 
Bluetooth protocol
Bluetooth protocolBluetooth protocol
Bluetooth protocol
 

Dernier

Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
Kamal Acharya
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
chumtiyababu
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
mphochane1998
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 

Dernier (20)

School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 

Rethrowing exception- JAVA

  • 1. Rethrowing Exception and User defined exception  Rajan Shah Name:
  • 2. Exception  Exception are of two types:  Synchronous exceptions- Errors such as “out- of-range index” and “overflow”.  Asynchronous exceptions- Errors that are caused by the events beyond the control of the program.  Purpose- to provide means to detect and report an “exceptional circumstance” so that appropriate action can be taken.
  • 3. Error handling code  The error handling code performs the following tasks: 1. Find the problem (Hit the exception), 2. Inform that an error has occurred (Throw the exception), 3. Receive the error info (Catch the exception) and 4. Take corrective action (Handle the exception).
  • 4. Exception handling mechanism  Try: The keyword try is used to preface a block of statements which may generate exceptions.  Throw: Exception is thrown using throw statement in try block.  Catch: Catches the exception thrown by the throw statement in the try block.
  • 5. Rethrowing an Exception  Rethrowing an expression from within an exception handler can be done by calling throw, by itself, with no exception (without arguments).  This causes the current exception to be passed on to an outer try/catch sequence.  An exception can only be rethrown from within a catch block.  When an exception is rethrown, it is propagated outward to the next catch block.
  • 6. Rethrowing an Exception  #include <iostream> using namespace std; void MyHandler() { try { throw “hello”; } catch (const char*) { cout <<”Caught exception inside MyHandlern”; throw; //rethrow char* out of function } }  int main() { cout<< “Main start”; try { MyHandler(); } catch(const char*) { cout <<”Caught exception inside Mainn”; } cout << “Main end”; return 0; }
  • 7. User Defined Exception  #include <iostream> #include <exception> using namespace std; struct MyException : public exception { const char * what () const throw () { return "C++ Exception"; } }; int main() { try { throw MyException(); } catch(MyException& e) { std::cout << "MyException caught" << std::endl; std::cout << e.what() << std::endl; } }
  • 8. User Defined Exception  The example shows the use of std::exception class to implement user defined exception.  what() -- is a public method provided by exception class and it has been overridden by all the child exception classes. This returns the cause of an exception.