0% found this document useful (0 votes)
4 views2 pages

Exception Handling Unit2 Notes

Exception handling is a mechanism to manage runtime errors that disrupt program execution, improving reliability and preventing crashes. It utilizes constructs like try-catch blocks, finally blocks, and keywords such as throw and throws to manage exceptions effectively. Best practices include writing specific exceptions, using meaningful messages, and creating custom exceptions when necessary.

Uploaded by

dkushvaha485
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)
4 views2 pages

Exception Handling Unit2 Notes

Exception handling is a mechanism to manage runtime errors that disrupt program execution, improving reliability and preventing crashes. It utilizes constructs like try-catch blocks, finally blocks, and keywords such as throw and throws to manage exceptions effectively. Best practices include writing specific exceptions, using meaningful messages, and creating custom exceptions when necessary.

Uploaded by

dkushvaha485
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

Unit 2 – Exception Handling (Complete Notes)

What is Exception?

An exception is a runtime error that disrupts the normal flow of program execution.

1 Divide by zero
2 Invalid array index
3 File not found

Features

1 Handles abnormal conditions

2 Prevents program crash

3 Improves reliability

4 Maintains normal flow

Why Exception Handling?

1 Prevents sudden termination

2 Maintains flow of program

3 Improves software reliability

4 Separates error handling code

Try-Catch Block

1 try contains risky code

2 catch handles exception

3 Multiple catch blocks allowed

4 Nested try also possible

Finally Block

1 Always executes

2 Used for cleanup

3 Runs even after return

4 May not run in [Link]()


throw Keyword

1 Used to throw exception manually


2 Only one exception at a time
3 Used for custom validation

throws Keyword

1 Declares exception in method


2 Used for checked exceptions
3 Passes exception to caller

Checked vs Unchecked

1 Checked → compile-time
2 Unchecked → runtime
3 RuntimeException → unchecked

Best Practices

1 Write specific exception first

2 Use meaningful messages

3 Do not use empty catch

4 Keep try block small

Custom Exception

We can create our own exception by extending Exception class.

Important Methods

1 getMessage()
2 toString()
3 printStackTrace()

Conclusion

Exception handling improves program stability and prevents crashes.

You might also like