Main Content

La traduzione di questa pagina non è aggiornata. Fai clic qui per vedere l'ultima versione in inglese.

Gestione degli errori

Generare, catturare e rispondere agli avvisi e agli errori

Per rendere il codice maggiormente robusto, verificate i casi limite e le condizioni problematiche. L'approccio più semplice consiste nell'utilizzare una dichiarazione if o switch per verificare la presenza di una condizione specifica e quindi emettere un errore o un avviso. Le dichiarazioni try/catch consentono di catturare e rispondere a qualsiasi errore.

Sintassi del linguaggio MATLAB

try, catchEseguire le dichiarazioni e catturare gli errori risultanti

Funzioni

errorThrow error and display message
warningDisplay warning message
lastwarnLast warning message
assertThrow error if condition false
onCleanupCleanup tasks upon function completion

Argomenti

  • Issue Warnings and Errors

    To flag unexpected conditions when running a program, issue a warning. To flag fatal problems within the program, throw an error. Unlike warnings, errors halt the execution of a program.

  • Suppress Warnings

    Your program might issue warnings that do not always adversely affect execution. To avoid confusion, you can hide warning messages during execution by changing their states from 'on' to 'off'.

  • Restore Warnings

    You can save the warning current states, modify warning states, and restore the original warning states. This technique is useful if you temporarily turn off some warnings and later reinstate the original settings.

  • Change How Warnings Display

    You can control how warnings appear in MATLAB®, including the display of warning suppression information and stack traces.

  • Use try/catch to Handle Errors

    Use a try/catch statement to execute code after your program encounters an error.

  • Clean Up When Functions Complete

    It is a good programming practice to leave your program environment in a clean state that does not interfere with any other program code.

Informazioni complementari