Trac3 !! Posted October 25, 2009 Report Share Posted October 25, 2009 Sveiki Vēlējos nosakaidrot ar ko labāks un kādās situācijās lietot try/catch.. Līdz šim neesmu viņu izmantojis un neredzu īsti pielietojumu, bet noteikti ir tam mēŗkis, ja jau tads eksistē tik daudzās programmēšanas valodas.. Quote Link to comment Share on other sites More sharing options...
briedis Posted October 25, 2009 Report Share Posted October 25, 2009 Man jau liekas, ka php manuāli ļoti labs piemērs pielietojumam: <?php function inverse($x) { if (!$x) { throw new Exception('Division by zero.'); } else return 1/$x; } try { echo inverse(5) . "\n"; echo inverse(0) . "\n"; } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; } // Continue execution echo 'Hello World'; ?> Pats vari veidot savus kļūdu paziņojumus... Quote Link to comment Share on other sites More sharing options...
Kavacky Posted October 26, 2009 Report Share Posted October 26, 2009 Savus kļūdu paziņojumus var veidot arī bez try/throw/catch. Labums ir tajā, ka nevajag taisīt 100 if pārbaudes, dezorganizējot visu kodu, piemēram: if () { if () { if () { ... } else { error = 1 } } else { error = 2 } } else { error = 3 } Tā vietā taisam pārskatāmu: try { if ( ) { throw(1) } ... if () { throw(2) } } catch { if ( 1 ) { error = 1 } else if ( 2 ) { error = 2 } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.