0% found this document useful (0 votes)
15 views7 pages

CH 7 Object Oriented Programming Using Java Hindi Notes

The document discusses exception handling in Java, explaining the differences between compile-time errors and run-time errors, as well as checked and unchecked exceptions. It provides examples of how exceptions occur and how to handle them using keywords like try, catch, throw, and throws. Additionally, it outlines the distinctions between the throw and throws keywords in Java.

Uploaded by

muhammad ali
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views7 pages

CH 7 Object Oriented Programming Using Java Hindi Notes

The document discusses exception handling in Java, explaining the differences between compile-time errors and run-time errors, as well as checked and unchecked exceptions. It provides examples of how exceptions occur and how to handle them using keywords like try, catch, throw, and throws. Additionally, it outlines the distinctions between the throw and throws keywords in Java.

Uploaded by

muhammad ali
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

COVID -19 #Stay Home Stay Safe

Chapter-7
Exception Handling
Exception:-

जब program execut ion के वक्त कोई problem आ जाता है , तो उसे Except ion कहते है |
Except ion ये runt ime पर आता है |
Java में दो प्रकार के Errors होते है |

1. Compile-Time Error
2. Run-Time Error

Compile-Time Error : ये एक normal Errors है जो Compiler द्वारा आ जाता है | जब Program


में कही syntax में , कही curly brace, semi-colon या comma नहीं ददया जाता है , तो ये Compile-Time
पर Error occur होता है |
Run-Time Error : यहााँ Program successfully run होता है | ले दकन कु छ ऐसी internal errors
आ जाती है , जो interpreter द्वारा दी जाती है | इससे Program भी बं द हो जाता है |
Except ion के बारे में दे खे तो, जब Program compile-t ime पर successfully run होता है , तो
program में कु छ statements ऐसे होते दक वो Compiler द्वारा execute नहीं होते है , उसी वक्त interpreter
द्वारा उस statements के सम्बं दित वो एक object create करता है , जब object create दकया जाता है , तब
interpreter द्वारा Run-Time पर उसे throw दकया जाता है |
इससे ये समझ आता है दक, जब भी कभी ऐसी condit ion program में आती है, तब Except ion Handling
का इस्ते माल Program में दकया जाता है |
Difference between Exception and Error:
Exception : Exception Run-time पर आ जाता है , जब Program; में divided by zero पर Exception आ
जाता है |
Error : जब Progra m में Syntax error, format error या program; out of memory जाता है , तो Error आ
जाती है | ये errors; critical होती है | इन error को ढं ढ ना काफी कदिन होता है | ये Error Compile-time पर आ जाता है
|
Example for Normal Exception
Source Code :
class Sample{

public static void main(String args[]){

int a = 4/0;

Object Oriented programming using ‘JAVA’


COVID -19 #Stay Home Stay Safe

Output :
C:\Program Files\Java\jdk1.8.0_111\bin>javac [Link]

C:\Program Files\Java\jdk1.8.0_111\bin>java Sample

Exception in thread "main" [Link]: / by zero

at [Link]([Link])

Describing Output

ऊपर के progra m में दे खे तो ये Run-Time पर ArithmeticException ये Exception occur होता है | ये एक


User का ख़राब Progra mming का उदाहरण है | यहााँ पर Runtime पर User को warning दी जाती है | Program पर
आया हु आ Exception काफी अच्छे से समझ आता है |

Java Exception Classes

Object Oriented programming using ‘JAVA’


COVID -19 #Stay Home Stay Safe

Program में Exception Handling में दो प्रकार के काम करता है |

1. Checked Exception
2. Un-checked Exception

Checked Exception : Checked Exception ये compile time पर occur होता है | इसे Compile-time
Exception भी कहा जाता है | जब progra m में दकसी प्रकार की checked exception आती है तो User को compiler
द्वारा बताया जाता है दक आप उस Exception को handle करे | For Example,
IOException
ClassNotFoundException आदद.
Program में दे खे तो FileInputStrea m data को read करने के दलए इस्ते माल दकया गया है | FileInputStream से
FileNotFoundException; ये Exception occur होता है | ये सब compile-time पर होता है और
Programmer को Exception ha ndle करने के दलए बताया जाता है |

Object Oriented programming using ‘JAVA’


COVID -19 #Stay Home Stay Safe

Source Code :
import [Link];

class Sample{

public static void main(String args[]){

FileInputStream file = null;

file = new FileInputStream("[Link]");

Output :
C:\Program Files\Java\jdk1.8.0_111\bin>javac [Link]

[Link]: error: unreported exception FileNotFoundException; must be caught or declared to be


thrown

file = new FileInputStream("[Link]");

1 error

दनचे ददया हु आ progra m Exception Handling का है | जब कोई file found नहीं होती तो एक message display
होता है | Exception Handling के दलए try, catch का इस्ते माल दकया गया है |
Source Code :
import [Link];

import [Link];

class Sample{

public static void main(String args[]){

FileInputStream file = null;

try{

Object Oriented programming using ‘JAVA’


COVID -19 #Stay Home Stay Safe

file = new FileInputStream("[Link]");

catch(FileNotFoundException e){

[Link]("File is not Found.");

Output :
File is not Found.

Un-checked Exception : Un-checked Exception ये run ti me पर occur होता है | इसे Run-time


Exception भी कहा जाता है | इसे compile-time पर check नहीं दकया जाता |जब इसे programmer द्वारा इसे handle
नहीं दकया जाता तो JVM इसे handle कर दे ता है |
For Example,
ArithmeticException
NullPointerException
NumberFormatException आदद.

Source Code :
class Sample{

public static void main(String args[]){

int a = 4/0;

Output :
C:\Program Files\Java\jdk1.8.0_111\bin>javac [Link]

C:\Program Files\Java\jdk1.8.0_111\bin>java Sample

Exception in thread "main" [Link]: / by zero

at Sample.([Link])

Object Oriented programming using ‘JAVA’


COVID -19 #Stay Home Stay Safe

at [Link]([Link])

Exception Handling के दलए java में पां च keywords का इस्ते माल दकया जाता है |

1. try
2. catch
3. finally
4. throw
5. throws

Throw:-
In Java we have already defined exception classes such as ArithmeticException,
NullPointerException, ArrayIndexOutOfBounds exception etc. These exceptions are
set to trigger on different-2 conditions. For example when we divide a number by zero,
this triggers ArithmeticException, when we try to access the array element out of its
bounds then we get ArrayIndexOutOfBoundsException.

We can define our own set of conditions or rules and throw an exception explicitly using
throw keyword. For example, we can throw ArithmeticException when we divide
number by 5, or any other numbers, what we need to do is just set the condition and
throw any exception using throw keyword.

Syntax of throw keyword:

throw new exception_class("error message");

Throws:-
The Java throws keyword is used to declare an exception. It gives an information to the
programmer that there may occur an exception so it is better for the programmer to provide the
exception handling code so that normal flow can be maintained. Exception Handling is mainly
used to handle the checked exceptions.

As we know that there are two types of exception checked and unchecked. Checked
exception (compile time) force you to handle them, if you don’t handle them then the
program will not compile.
On the other hand unchecked exception (Runtime) doesn’t get checked during
compilation. Throws keyword is used for handling checked exceptions. By using
throws we can declare multiple exceptions in one go.

Object Oriented programming using ‘JAVA’


COVID -19 #Stay Home Stay Safe

Difference between throw and throws in Java


There are many differences between throw and throws keywords. A list of differences
between throw and throws are given below:

No. throw throws

1) Java throw keyword is used to explicitly Java throws keyword is used to declare an
throw an exception. exception.

2) Checked exception cannot be propagated Checked exception can be propagated with


using throw only. throws.

3) Throw is followed by an instance. Throws is followed by class.

4) Throw is used within the method. Throws is used with the method signature.

5) You cannot throw multiple exceptions. You can declare multiple exceptions e.g.
public void method()throws
IOException,SQLException.

Object Oriented programming using ‘JAVA’

You might also like