SlideShare uma empresa Scribd logo
Java Session 5
•

Exceptions & Assertions
custom exception : https://gist.github.com/rajeevprasanna/9166347

• Return statement in finally block override the one returned from try block.
Ex : https://gist.github.com/rajeevprasanna/9166354
Assertions :
•

assertions let you test your assumptions during development, without the expense (in both your time
and program overhead) of writing exception handlers for exceptions that you assume will never
happen once the program is out of development and fully deployed.

•

While testing and debugging, you want to validate your assumption, but you don't want to have to
strip out print statements, runtime exception handlers, or if/else tests when you're done with
development. But leaving any of those in is, at the least, a performance hit. Assertions to the rescue!

•

Not only do assertions let your code stay cleaner and tighter, but because assertions are inactive
unless specifically "turned on" (enabled), the code will run as without assertions

•

You always assert that something is true. If it is, no problem. Code keeps running. But if your
assertion turns out to be wrong (false), then a stop-the-world AssertionError is thrown (that you
should never, ever handle!) right then and there, so you can fix whatever logic flaw led to the problem

•

Assertions are typically enabled when an application is being tested and debugged, but disabled when
the application is deployed. The assertions are still in the code, although ignored by the JVM, so if
you do have a deployed application that starts misbehaving, you can always choose to enable
assertions in the field for additional testing.
•

Assertions(contd …)
Enabling assertions : You enable assertions at runtime with :
•java -ea com.geeksanonymous.TestClass
•java -enableassertions com.geeksanonymous.TestClass
•The preceding command-line switches tell the JVM to run with assertions enabled

•

Disabling assertions at runtime :
• java -da com.geeksanonymous.TestClass
•java -disableassertions com.geeksanonymous.TestClass
Ex : https://gist.github.com/rajeevprasanna/9166931

•

you're never supposed to handle an assertion failure. That means you shouldn't catch it with a catch clause and
attempt to recover. Legally, however, AssertionError is a subclass of Throwable, so it can be caught. But just don't
do it! If you're going to try to recover from something, it should be an exception. To discourage you from trying to
substitute an assertion for an exception, the AssertionError doesn't provide access to the object(e) that generated it.
All you get is the String message.

•

Don't Use Assertions to Validate Arguments to a Public Method

•

Do Use Assertions to Validate Arguments to a Private Method

•

Don't Use Assertions to Validate Command-Line Arguments

•

Do Use Assertions, Even in Public Methods, to Check for Cases that You Know Are Never, Ever Supposed to
Happen

•

Don't Use Assert Expressions that Can Cause Side Effects!

•

Using assertions that cause side effects can cause some of the most maddening and hard-to-find bugs known to
man! When a hot tempered Q.A. analyst is screaming at you that your code doesn't work, trotting out the old "well
it works on MY machine" excuse won't get you very far.
Javasession5

Mais conteúdo relacionado

Mais procurados

Mais procurados (9)

Behaviour-Driven Development, Ruby Style
Behaviour-Driven Development, Ruby StyleBehaviour-Driven Development, Ruby Style
Behaviour-Driven Development, Ruby Style
 
Live Testing A Legacy App
Live Testing A Legacy AppLive Testing A Legacy App
Live Testing A Legacy App
 
Owning a company through their logs
Owning a company through their logsOwning a company through their logs
Owning a company through their logs
 
Unit Testing Lightning Components with Jasmine
Unit Testing Lightning Components with JasmineUnit Testing Lightning Components with Jasmine
Unit Testing Lightning Components with Jasmine
 
Journey to the Center of Ember Test Helpers
Journey to the Center of Ember Test HelpersJourney to the Center of Ember Test Helpers
Journey to the Center of Ember Test Helpers
 
wtst3_pettichord3
wtst3_pettichord3wtst3_pettichord3
wtst3_pettichord3
 
Wix Automation - The False Positive Paradox
Wix Automation - The False Positive ParadoxWix Automation - The False Positive Paradox
Wix Automation - The False Positive Paradox
 
Testing JSF with Arquillian and Selenium
Testing JSF with Arquillian and SeleniumTesting JSF with Arquillian and Selenium
Testing JSF with Arquillian and Selenium
 
Arquillian: Effective tests from the client to the server
Arquillian: Effective tests from the client to the serverArquillian: Effective tests from the client to the server
Arquillian: Effective tests from the client to the server
 

Destaque (7)

Multi threaded rtos
Multi threaded rtosMulti threaded rtos
Multi threaded rtos
 
Multi-Threading
Multi-ThreadingMulti-Threading
Multi-Threading
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Threads and multi threading
Threads and multi threadingThreads and multi threading
Threads and multi threading
 
Operating Systems
Operating SystemsOperating Systems
Operating Systems
 
Operating system overview concepts ppt
Operating system overview concepts pptOperating system overview concepts ppt
Operating system overview concepts ppt
 
Segmentation in Operating Systems.
Segmentation in Operating Systems.Segmentation in Operating Systems.
Segmentation in Operating Systems.
 

Semelhante a Javasession5

Semelhante a Javasession5 (20)

Javasession4
Javasession4Javasession4
Javasession4
 
Java Exception Handling
Java Exception HandlingJava Exception Handling
Java Exception Handling
 
[FullStack NYC 2019] Effective Unit Tests for JavaScript
[FullStack NYC 2019] Effective Unit Tests for JavaScript[FullStack NYC 2019] Effective Unit Tests for JavaScript
[FullStack NYC 2019] Effective Unit Tests for JavaScript
 
Pi j4.2 software-reliability
Pi j4.2 software-reliabilityPi j4.2 software-reliability
Pi j4.2 software-reliability
 
The Many Ways to Test Your React App
The Many Ways to Test Your React AppThe Many Ways to Test Your React App
The Many Ways to Test Your React App
 
exceptions in java
exceptions in javaexceptions in java
exceptions in java
 
Chap2 exception handling
Chap2 exception handlingChap2 exception handling
Chap2 exception handling
 
Java-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handlingJava-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handling
 
Control statements
Control statementsControl statements
Control statements
 
JavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and KarmaJavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and Karma
 
exception handling in java.ppt
exception handling in java.pptexception handling in java.ppt
exception handling in java.ppt
 
Test Dependencies and the Future of Build Acceleration
Test Dependencies and the Future of Build AccelerationTest Dependencies and the Future of Build Acceleration
Test Dependencies and the Future of Build Acceleration
 
A36519192_21789_4_2018_Exception Handling.ppt
A36519192_21789_4_2018_Exception Handling.pptA36519192_21789_4_2018_Exception Handling.ppt
A36519192_21789_4_2018_Exception Handling.ppt
 
Token Testing Slides
Token  Testing SlidesToken  Testing Slides
Token Testing Slides
 
Exception handling in java-PPT.pptx
Exception handling in java-PPT.pptxException handling in java-PPT.pptx
Exception handling in java-PPT.pptx
 
Unit testing of java script and angularjs application using Karma Jasmine Fra...
Unit testing of java script and angularjs application using Karma Jasmine Fra...Unit testing of java script and angularjs application using Karma Jasmine Fra...
Unit testing of java script and angularjs application using Karma Jasmine Fra...
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End Development
 
Testing w-mocks
Testing w-mocksTesting w-mocks
Testing w-mocks
 
OCA Java SE 8 Exam Chapter 6 Exceptions
OCA Java SE 8 Exam Chapter 6 ExceptionsOCA Java SE 8 Exam Chapter 6 Exceptions
OCA Java SE 8 Exam Chapter 6 Exceptions
 
Qt test framework
Qt test frameworkQt test framework
Qt test framework
 

Mais de Rajeev Kumar (8)

Db performance optimization with indexing
Db performance optimization with indexingDb performance optimization with indexing
Db performance optimization with indexing
 
Javasession10
Javasession10Javasession10
Javasession10
 
Jms intro
Jms introJms intro
Jms intro
 
Javasession8
Javasession8Javasession8
Javasession8
 
Javasession6
Javasession6Javasession6
Javasession6
 
Javasession7
Javasession7Javasession7
Javasession7
 
Java session 3
Java session 3Java session 3
Java session 3
 
Java session2
Java session2Java session2
Java session2
 

Último

IATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdffIATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdff
17thcssbs2
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptx
heathfieldcps1
 

Último (20)

Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdfPost Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
 
“O BEIJO” EM ARTE .
“O BEIJO” EM ARTE                       .“O BEIJO” EM ARTE                       .
“O BEIJO” EM ARTE .
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
IATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdffIATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdff
 
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General QuizPragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptx
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 

Javasession5

  • 2. • Exceptions & Assertions custom exception : https://gist.github.com/rajeevprasanna/9166347 • Return statement in finally block override the one returned from try block. Ex : https://gist.github.com/rajeevprasanna/9166354 Assertions : • assertions let you test your assumptions during development, without the expense (in both your time and program overhead) of writing exception handlers for exceptions that you assume will never happen once the program is out of development and fully deployed. • While testing and debugging, you want to validate your assumption, but you don't want to have to strip out print statements, runtime exception handlers, or if/else tests when you're done with development. But leaving any of those in is, at the least, a performance hit. Assertions to the rescue! • Not only do assertions let your code stay cleaner and tighter, but because assertions are inactive unless specifically "turned on" (enabled), the code will run as without assertions • You always assert that something is true. If it is, no problem. Code keeps running. But if your assertion turns out to be wrong (false), then a stop-the-world AssertionError is thrown (that you should never, ever handle!) right then and there, so you can fix whatever logic flaw led to the problem • Assertions are typically enabled when an application is being tested and debugged, but disabled when the application is deployed. The assertions are still in the code, although ignored by the JVM, so if you do have a deployed application that starts misbehaving, you can always choose to enable assertions in the field for additional testing.
  • 3. • Assertions(contd …) Enabling assertions : You enable assertions at runtime with : •java -ea com.geeksanonymous.TestClass •java -enableassertions com.geeksanonymous.TestClass •The preceding command-line switches tell the JVM to run with assertions enabled • Disabling assertions at runtime : • java -da com.geeksanonymous.TestClass •java -disableassertions com.geeksanonymous.TestClass Ex : https://gist.github.com/rajeevprasanna/9166931 • you're never supposed to handle an assertion failure. That means you shouldn't catch it with a catch clause and attempt to recover. Legally, however, AssertionError is a subclass of Throwable, so it can be caught. But just don't do it! If you're going to try to recover from something, it should be an exception. To discourage you from trying to substitute an assertion for an exception, the AssertionError doesn't provide access to the object(e) that generated it. All you get is the String message. • Don't Use Assertions to Validate Arguments to a Public Method • Do Use Assertions to Validate Arguments to a Private Method • Don't Use Assertions to Validate Command-Line Arguments • Do Use Assertions, Even in Public Methods, to Check for Cases that You Know Are Never, Ever Supposed to Happen • Don't Use Assert Expressions that Can Cause Side Effects! • Using assertions that cause side effects can cause some of the most maddening and hard-to-find bugs known to man! When a hot tempered Q.A. analyst is screaming at you that your code doesn't work, trotting out the old "well it works on MY machine" excuse won't get you very far.