Can someone explain to me the use of throw in exception handling? What happens when I make an exception?
This means throwing an exception. When you “throw” an exception, you say “something went wrong, here are some details.”
Then you can “catch” the “thrown” exception so that your program can correctly degrade, instead of mistakenly dying.
An “throw” of an exception is what triggers the entire process of handling exceptions.
. , .
, "" , "catch". catch catch ( "" ), catch.
// Do some stuff, an exception thrown here won't be caught. try { // Do stuff throw new InvalidOperationException("Some state was invalid."); // Nothing here will be executed because the exception has been thrown } catch(InvalidOperationException ex) // Catch and handle the exception { // This code is responsible for dealing with the error condition // that prompted the exception to be thrown. We choose to name // the exception "ex" in this block. } // This code will continue to execute as usual because the exception // has been handled.
, , - , . , - ( ).
, , catch, , . finally , , ( ) , , , .
. , , , .
, - , .
. throw.
throw
,
if(inputVal < 0) { throw new LessThanZeroCustomException("You cannot enter a value less than zero"); }
, LessThanZeroCustomException. , Custom , . ,
LessThanZeroCustomException
Custom
, . - . , , . , try ... catch . !
try ... catch
In short, it throwmeans "I found an exceptional condition that I cannot handle, so I let the person using this code know by throwing an exception."
Source: https://habr.com/ru/post/1703617/More articles:user authenticated but missing Ticket.UserData - c #Lack of Libtool, double building? - autotoolsCPython internal structures - pythonCamel SOAP Method Names - soapRetrieving username from OpenID - authenticationhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1703618/convert-characters-to-html-entities-in-cocoa&usg=ALkJrhha4xTT2FKjh2Vr09OB4lbnx6y3fwUnable to serve pages on Mac OS X using Apache default web server - macosHow to implement gmail login? - authenticationCan I embed sqlite database in an Air application? - sqliteMessage Queuing in Message Queuing - msmqAll Articles