• Guest
HabraHabr
  • Main
  • Users

  • Development
    • Programming
    • Information Security
    • Website development
    • JavaScript
    • Game development
    • Open source
    • Developed for Android
    • Machine learning
    • Abnormal programming
    • Java
    • Python
    • Development of mobile applications
    • Analysis and design of systems
    • .NET
    • Mathematics
    • Algorithms
    • C#
    • System Programming
    • C++
    • C
    • Go
    • PHP
    • Reverse engineering
    • Assembler
    • Development under Linux
    • Big Data
    • Rust
    • Cryptography
    • Entertaining problems
    • Testing of IT systems
    • Testing Web Services
    • HTML
    • Programming microcontrollers
    • API
    • High performance
    • Developed for iOS
    • CSS
    • Industrial Programming
    • Development under Windows
    • Image processing
    • Compilers
    • FPGA
    • Professional literature
    • OpenStreetMap
    • Google Chrome
    • Data Mining
    • PostgreSQL
    • Development of robotics
    • Visualization of data
    • Angular
    • ReactJS
    • Search technologies
    • Debugging
    • Test mobile applications
    • Browsers
    • Designing and refactoring
    • IT Standards
    • Solidity
    • Node.JS
    • Git
    • LaTeX
    • SQL
    • Haskell
    • Unreal Engine
    • Unity3D
    • Development for the Internet of things
    • Functional Programming
    • Amazon Web Services
    • Google Cloud Platform
    • Development under AR and VR
    • Assembly systems
    • Version control systems
    • Kotlin
    • R
    • CAD/CAM
    • Customer Optimization
    • Development of communication systems
    • Microsoft Azure
    • Perfect code
    • Atlassian
    • Visual Studio
    • NoSQL
    • Yii
    • Mono и Moonlight
    • Parallel Programming
    • Asterisk
    • Yandex API
    • WordPress
    • Sports programming
    • Lua
    • Microsoft SQL Server
    • Payment systems
    • TypeScript
    • Scala
    • Google API
    • Development of data transmission systems
    • XML
    • Regular expressions
    • Development under Tizen
    • Swift
    • MySQL
    • Geoinformation services
    • Global Positioning Systems
    • Qt
    • Dart
    • Django
    • Development for Office 365
    • Erlang/OTP
    • GPGPU
    • Eclipse
    • Maps API
    • Testing games
    • Browser Extensions
    • 1C-Bitrix
    • Development under e-commerce
    • Xamarin
    • Xcode
    • Development under Windows Phone
    • Semantics
    • CMS
    • VueJS
    • GitHub
    • Open data
    • Sphinx
    • Ruby on Rails
    • Ruby
    • Symfony
    • Drupal
    • Messaging Systems
    • CTF
    • SaaS / S+S
    • SharePoint
    • jQuery
    • Puppet
    • Firefox
    • Elm
    • MODX
    • Billing systems
    • Graphical shells
    • Kodobred
    • MongoDB
    • SCADA
    • Hadoop
    • Gradle
    • Clojure
    • F#
    • CoffeeScript
    • Matlab
    • Phalcon
    • Development under Sailfish OS
    • Magento
    • Elixir/Phoenix
    • Microsoft Edge
    • Layout of letters
    • Development for OS X
    • Forth
    • Smalltalk
    • Julia
    • Laravel
    • WebGL
    • Meteor.JS
    • Firebird/Interbase
    • SQLite
    • D
    • Mesh-networks
    • I2P
    • Derby.js
    • Emacs
    • Development under Bada
    • Mercurial
    • UML Design
    • Objective C
    • Fortran
    • Cocoa
    • Cobol
    • Apache Flex
    • Action Script
    • Joomla
    • IIS
    • Twitter API
    • Vkontakte API
    • Facebook API
    • Microsoft Access
    • PDF
    • Prolog
    • GTK+
    • LabVIEW
    • Brainfuck
    • Cubrid
    • Canvas
    • Doctrine ORM
    • Google App Engine
    • Twisted
    • XSLT
    • TDD
    • Small Basic
    • Kohana
    • Development for Java ME
    • LiveStreet
    • MooTools
    • Adobe Flash
    • GreaseMonkey
    • INFOLUST
    • Groovy & Grails
    • Lisp
    • Delphi
    • Zend Framework
    • ExtJS / Sencha Library
    • Internet Explorer
    • CodeIgniter
    • Silverlight
    • Google Web Toolkit
    • CakePHP
    • Safari
    • Opera
    • Microformats
    • Ajax
    • VIM
  • Administration
    • System administration
    • IT Infrastructure
    • *nix
    • Network technologies
    • DevOps
    • Server Administration
    • Cloud computing
    • Configuring Linux
    • Wireless technologies
    • Virtualization
    • Hosting
    • Data storage
    • Decentralized networks
    • Database Administration
    • Data Warehousing
    • Communication standards
    • PowerShell
    • Backup
    • Cisco
    • Nginx
    • Antivirus protection
    • DNS
    • Server Optimization
    • Data recovery
    • Apache
    • Spam and antispam
    • Data Compression
    • SAN
    • IPv6
    • Fidonet
    • IPTV
    • Shells
    • Administering domain names
  • Design
    • Interfaces
    • Web design
    • Working with sound
    • Usability
    • Graphic design
    • Design Games
    • Mobile App Design
    • Working with 3D-graphics
    • Typography
    • Working with video
    • Work with vector graphics
    • Accessibility
    • Prototyping
    • CGI (graphics)
    • Computer Animation
    • Working with icons
  • Control
    • Careers in the IT industry
    • Project management
    • Development Management
    • Personnel Management
    • Product Management
    • Start-up development
    • Managing the community
    • Service Desk
    • GTD
    • IT Terminology
    • Agile
    • Business Models
    • Legislation and IT-business
    • Sales management
    • CRM-systems
    • Product localization
    • ECM / EDS
    • Freelance
    • Venture investments
    • ERP-systems
    • Help Desk Software
    • Media management
    • Patenting
    • E-commerce management
    • Creative Commons
  • Marketing
    • Conferences
    • Promotion of games
    • Internet Marketing
    • Search Engine Optimization
    • Web Analytics
    • Monetize Web services
    • Content marketing
    • Monetization of IT systems
    • Monetize mobile apps
    • Mobile App Analytics
    • Growth Hacking
    • Branding
    • Monetize Games
    • Display ads
    • Contextual advertising
    • Increase Conversion Rate
  • Sundry
    • Reading room
    • Educational process in IT
    • Research and forecasts in IT
    • Finance in IT
    • Hakatonas
    • IT emigration
    • Education abroad
    • Lumber room
    • I'm on my way

How to handle errors on JVM faster

There are various ways to handle errors in programming languages: 3r33371.
 
 
standard exceptions for many languages ​​(Java, Scala and other JVM, python, and many others) 3r3105.  
status codes or flags (Go, bash)
 
various algebraic data structures, the values ​​of which can be both successful results and error descriptions (Scala, haskell, and other functional languages) 3r3105.  
 
Exceptions are used very widely, on the other hand they are often said to be slow. But opponents of the functional approach often appeal to performance. 3r33333.
 
Recently, I have been working with Scala, where I can equally use both exceptions and different types of data for error handling, so I wonder which approach will be more convenient and faster. 3r33333.
 
Immediately discard the use of codes and flags, since this approach is not accepted in the JVM languages ​​and in my opinion too prone to errors (I apologize for the pun). Therefore, we will compare exceptions and different types of ATD. In addition, ADT can be considered as the use of error codes in a functional style. 3r33333.
 
3r3111. UPDATE 3r3–3112. : added exceptions without stack traces to 3x3r3371. 3r3338.
 
3r3342. Contestants
 
A little more about algebraic data types [/b]
For those who are not very familiar with ADT (3r3351. ADT 3r33285.) - the algebraic type consists of several possible values, each of which can be a composite value (structure, record). 3r33333.
 
An example is the type Option[T]= Some (value: T) | None , which is used instead of null: value of this type can be either Some (t) if the value is, or None if it is not. 3r33333.
 

Another example could be Try[T]= Success (value: T) | Failure (exception: Throwable) , which describes the result of the calculation, which could be completed successfully or with an error. 3r33333.
 

So our contestants:


 
  •  
  • The old Good 3r3328. exceptions  
  • There are exceptions without a stack trace, since it is the filling of the stack trace that is a very slow operation  
  • Try[T]= Success (value: T) | Failure (exception: Throwable) - the same exceptions, but in a functional wrapper  
  • Either[String, T]= Left (error: String) | Right (value: T) - A type containing either a result or a description of the error  
  • ValidatedNec[String, T]= Valid (value: T) | Invalid (errors: List[String]) - type of Cats libraries. which, in the event of an error, may contain several messages about different errors (it does not use exactly List , but this is not important)  

 

3r3111. NOTE 3r3-33112. in fact, exceptions are compared with stack-based, without and ATD, but several types are chosen, since Scala does not have a unified approach and it is interesting to compare several. 3r33333.
 

In addition to exceptions, strings are used here to describe errors, but with the same success, different classes would be used in a real situation ( Either[Failure, T]3rr3370.). 3r33333.
 
3r3122. The problem is
 

For testing error handling, let's take the problem of parsing and validating dаta: 3r-3371.
 

    case class Person (name: String, age: Int, isMale: Boolean)
type Result[T]= Either[String, T]
trait PersonParser {
def parse (dаta: Map[String, String]): Result[Person]
}
3r3304.
 

those. having raw data Map[String, String] need to get Person or an error if the data is not valid. 3r33333.
 
3r33150. Throw
 

Solution

head on with the use of exceptions (hereinafter I will only give the function 3r36969. person , the full code can be found at 3r3-3159. github ):
 
ThrowParser.scala 3r33333.
 
    def person (dаta: Map[String, String]): Person = {3r3333378. val name = string (data.getOrElse ("name", null))
val age = integer (data.getOrElse ("age", null))
val isMale = boolean (data.getOrElse ("isMale", null))
require (name.nonEmpty, "name should not be empty")
require (age> ? "age should be positive")
Person (name, age, isMale)
}
3r3304.
 

here 3r33369. string , integer and boolean validates the presence and format of simple types and performs the conversion.
 
In general, quite simple and understandable. 3r33333.
 
3r3193. ThrowNST (No Stack Trace)
 

The code is the same as in the previous case, but exceptions are used without the stack trace where it is possible: ThrowNSTParser.scala 3r33333.
 

Try


 

The solution catches exceptions earlier and allows you to combine the results through 3r33369. for (not to be confused with cycles in other languages):
 
TryParser.scala 3r33333.
 

    def person (dаta: Map[String, String]): Try[Person]= for {
name <- required(data.get("name"))
age <- required(data.get("age")) flatMap integer
isMale <- required(data.get("isMale")) flatMap boolean
_ <- require(name.nonEmpty, "name should not be empty")
_ <- require(age > ? "age should be positive")
} yield Person (name, age, isMale)
3r3304.
 

a bit more unusual for a weak eye, but at the expense of using for In general, it is very similar to the version with exceptions, besides, the validation of the presence of the field and the parsing of the desired type occur separately ( flatMap here you can read as 3r33333. and then ) 3r3333371.
 

Either


 

There is a type Either hidden behind the alias Result since the type of error is fixed:
 
EitherParser.scala 3r33333.
 

    def person (dаta: Map[String, String]): Result[Person]= for {
name <- required(data.get("name"))
age <- required(data.get("age")) flatMap integer
isMale <- required(data.get("isMale")) flatMap boolean
_ <- require(name.nonEmpty, "name should not be empty")
_ <- require(age > ? "age should be positive")
} yield Person (name, age, isMale)
3r3304.
 

Since the standard Either as Try forms a monad in Scala, then the code came out exactly the same, the difference here is that the error appears here as a string and the exceptions are used minimally (only for error handling when parsing the number) 3r3371.
 

Validated


 

Here the Cats library is used in order to get in the event of an error not the first thing that happened, but as much as possible (for example, if several fields were not valid, then the result will contain errors of parsing all these fields) 3r3333366.  
ValidatedParser.scala 3r33333.
 

    def person (dаta: Map[String, String]): Validated[Person]= {
val name: Validated[String]=
required (data.get ("name")) 3r33333. .ensure (one ("name should not be empty")) (_. nonEmpty)
val age: Validated[Int]=
required (data.get ("age")) 3r3333378. .andThen (integer)
.ensure (one ("age should be positive")) (_> 0)
val isMale: Validated[Boolean]=
required (data.get ("isMale"))
.andThen (boolean)
(name, age, isMale) .mapN (Person)
}
3r3304.
 

This code is less similar to the original version with exceptions, but checking for additional restrictions is not divorced from parsing fields, and we still get a few errors instead of one, it's worth it! 3r33333.
 
3r33333. Testing
 

For testing, a set of data was generated with a different percentage of errors and parsed in each of the methods. 3r33333.
 

Result on all percent of errors:
 
How to handle errors on JVM faster  

In more detail on a low percentage of errors (time is different here since it was used 3-???. About 3r3328. A sample): 3r-3366.  
 

If any part of the errors is an exception with the stack trace (in our case, the error of parsing the number will be an exception that we do not control), then of course the performance of the “fast” error handling methods will significantly deteriorate. Especially suffering Validated , since it collects all errors and as a result receives a slow exception more than others:
 
3r33333. 3r33333.
 

Conclusions


 

As the experiment showed, exceptions with stack-traces are really very slow (100% error is the difference between 3r3693? Throw

? and 3r3333369. Either More than 50 times!), And when there are almost no exceptions, the use of ADT has its price. However, using exceptions without stack traces is as fast (and with a low percentage of errors faster) as ADT, but if such exceptions go beyond the same validation, it will not be easy to track their source. 3r33333.
 

So, if the probability of an exception is more than 1%, exceptions without stack traces work the fastest, Validated or the usual Either almost as fast. With a large number of errors Either may be slightly faster Validated only due to fail-fast semantics. 3r33333.
 

The use of ADT for error handling gives another advantage over exceptions: the possibility of an error is sewn into the type itself and is harder to miss, as with the use of 3r36969. Option instead of nulls. 3r33333.

It may be interesting

  • Comments
  • About article
  • Similar news
This publication has no comments.

weber

Author

7-12-2018, 07:32

Publication Date

JavaScript / Scala / Programming / Functional Programming

Category
  • Comments: 0
  • Views: 324
How to work with exceptions in DDD
What will happen to error handling in C
We invite you to hackathon JETHACK
Corda: Kotlin
We understand implicit'y in Scala
Scala - from the dead donkey ears
Write a comment
Name:*
E-Mail:


Comments
Global Dezigns is a Website Development Company in Karachi, Providing services of
website design in karachi
. We are delivering the best partnership across Pakistan. provides a complete range of web development services including web applications, website hosting and maintenance, domain registration, on-page search engine optimization, and website integration with social media platforms such as Facebook, Twitter, LinkedIn, Google Maps, and Google Local Directory. We believe we are well placed to take our knowledge and expertise to the logical next level with the latest web standards.  
  Show/hide text
https://www.globaldezigns.com/



Yesterday, 22:45

mike tomlin

This blog is really great. The information here will surely be of some help to me. Thanks!.mastering physics

Yesterday, 17:57

raymond weber

Coinyspace is the cryptocurrency community and trading forum where members can find any contributors of crypto ecosystem like currencies, exchanges & merchants. Check Out: Bitcoin Merchants
Yesterday, 16:57

noorseo

This is a great high resolution screen which you have shared for the users. Making a website is not an easy task but managing a good website is really a hard work. As far as this website is concerned, I am very happy.https://19216801.1
Yesterday, 16:01

nushra45

i'm satisfied to deem this make recognized Very beneficial for me, as it contains lot of advice. I usually favor to buttonhole The excellent and glad i found this count number in you assertion. thanks Subliminal Hypnosis
Yesterday, 14:56

jahangirkhatri

Adv
Website for web developers. New scripts, best ideas, programming tips. How to write a script for you here, we have a lot of information about various programming languages. You are a webmaster or a beginner programmer, it does not matter, useful articles will help to make your favorite business faster.

Login

Registration Forgot password