SlideShare une entreprise Scribd logo
1  sur  4
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

Contenu connexe

Tendances

Tendances (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
 

En vedette (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.
 

Similaire à Javasession5

Similaire à 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
 

Plus 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
 

Dernier

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Dernier (20)

How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 

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.