"예외 처리 구문"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(사용자 2명의 중간 판 5개는 보이지 않습니다)
4번째 줄: 4번째 줄:
== Bash ==
== Bash ==
[[category: Bash]]
[[category: Bash]]
<source lang='Bash'>
<syntaxhighlight lang='Bash'>
#!/usr/bin/env bash
#!/usr/bin/env bash
#set -e provides another error mechanism
#set -e provides another error mechanism
16번째 줄: 16번째 줄:
echo oops)
echo oops)
echo never printed
echo never printed
</source>
</syntaxhighlight>


==C==
==C==
[[category: C]]
[[category: C]]
<source lang=C>
<syntaxhighlight lang=C>
#include <setjmp.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdio.h>
48번째 줄: 48번째 줄:
   return EXIT_SUCCESS;
   return EXIT_SUCCESS;
}
}
</source>
</syntaxhighlight>


== C# ==
== C# ==
[[category:csharp]]
[[category:csharp]]
<source lang=CSharp>
<syntaxhighlight lang=CSharp>
public static void Main()
public static void Main()
{
{
77번째 줄: 77번째 줄:
   }
   }
}
}
</source>
</syntaxhighlight>


=== [[C++]] ===
== [[C++]] ==
[[category: C++]]
[[category: C++]]
<source lang=Cpp>
<syntaxhighlight lang=Cpp>
#include <exception>
#include <exception>
int main() {
int main() {
95번째 줄: 95번째 줄:
   }
   }
}
}
</source>
</syntaxhighlight>


== Java ==
== Java ==
[[category: java]]
[[category: java]]
<source lang=Java>
<syntaxhighlight lang=Java>
try {
try {
   // Normal execution path
   // Normal execution path
109번째 줄: 109번째 줄:
   //  except when System.exit() is called in "try" or "catch" blocks;
   //  except when System.exit() is called in "try" or "catch" blocks;
}
}
</source>
</syntaxhighlight>


=== [[JavaScript]] ===
== [[JavaScript]] ==
[[category: JavaScript]]
[[category: JavaScript]]
<source lang=JavaScript>
<syntaxhighlight lang=JavaScript>
try {
try {
   // Statements in which exceptions might be thrown
   // Statements in which exceptions might be thrown
122번째 줄: 122번째 줄:
   // Statements that execute afterward either way
   // Statements that execute afterward either way
}
}
</source>
</syntaxhighlight>


== [[PHP]] ==
== [[PHP]] ==
[[category:PHP]]
[[category:PHP]]
<source lang=PHP>
<syntaxhighlight lang=PHP>
// Exception handling is only available in PHP versions 5 and greater.
// Exception handling is only available in PHP versions 5 and greater.
try
try
141번째 줄: 141번째 줄:
   // you get the idea what i mean ;)
   // you get the idea what i mean ;)
}
}
</source>
</syntaxhighlight>
 
== Python ==
[[분류: Python]]
<syntaxhighlight lang=Python>
f = None
try:
  f = file("aFileName")
  f.write(could_make_error())
except IOError:
  print "Unable to open file"
except:  # catch all exceptions
  print "Unexpected error"
else:    # executed if no exceptions are raised
  print "File write completed successfully"
finally:  # clean-up actions, always executed
  if f:
      f.close()
</syntaxhighlight>


==같이 보기==
==같이 보기==
147번째 줄: 165번째 줄:
*[[try-catch]]
*[[try-catch]]


==참고 자료==
==참고==
*http://en.wikipedia.org/wiki/Exception_handling_syntax
*http://en.wikipedia.org/wiki/Exception_handling_syntax
[[분류:프로그래밍]]
[[분류:제어 흐름]]

2020년 11월 2일 (월) 02:31 기준 최신판

exception handling syntax
예외 처리 구문

1 Bash[ | ]

#!/usr/bin/env bash
#set -e provides another error mechanism
print_error(){
	echo "there was an error"
}
trap print_error exit #list signals to trap
tempfile=`mktemp`
trap "rm $tempfile" exit
./other.sh || echo warning: other failed
echo oops)
echo never printed

2 C[ | ]

#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>

enum { SOME_EXCEPTION = 1 } exception;
jmp_buf state;

int main(void)
{
  if(!setjmp(state))                      // try
  {
    if(/* something happened */)
    {
      exception = SOME_EXCEPTION;
      longjmp(state, 0);                  // throw SOME_EXCEPTION
    }
  } 
  else switch(exception)
  {             
    case SOME_EXCEPTION:                  // catch SOME_EXCEPTION
      puts("SOME_EXCEPTION caught");
      break;
    default:                              // catch ...
      puts("Some strange exception");
  }
  return EXIT_SUCCESS;
}

3 C#[ | ]

public static void Main()
{
   try
   {
      // Code that could throw an exception
   }
   catch(System.Net.WebException ex)
   {
      // Process a WebException
   }
   catch(System.Exception)
   {
      // Process a System level CLR exception, that is not a System.Net.WebException,
      // since the exception has not been given an identifier it cannot be referenced
   }
   catch
   {
      // Process a non-CLR exception
   }
   finally
   {
      // (optional) code that will *always* execute
   }
}

4 C++[ | ]

#include <exception>
int main() {
   try {
       // do something (might throw an exception)
   }
   catch (const std::exception& e) {
        // handle exception e
   }
   catch (...) {
        // catches all exceptions, not already catched by a catch block before
        // can be used to catch exception of unknown or irrelevant type
   }
}

5 Java[ | ]

try {
   // Normal execution path
   throw new EmptyStackException();
} catch (ExampleException ee) {
   //  deal with the ExampleException
} finally {
   // This optional section is executed upon termination of any of the try or catch blocks above, 
   //  except when System.exit() is called in "try" or "catch" blocks;
}

6 JavaScript[ | ]

try {
  // Statements in which exceptions might be thrown
  throw 'error';
} catch(error) {
  // Statements that execute in the event of an exception
} finally {
  // Statements that execute afterward either way
}

7 PHP[ | ]

// Exception handling is only available in PHP versions 5 and greater.
try
{
  // Code that might throw an exception
  throw new Exception('Invalid URL.');
}
catch (FirstExceptionClass $exception) 
{
  // Code that handles this exception
} 
catch (SecondExceptionClass $exception) 
{
  // you get the idea what i mean ;)
}

8 Python[ | ]

f = None
try:
   f = file("aFileName")
   f.write(could_make_error())
except IOError:
   print "Unable to open file"
except:   # catch all exceptions
   print "Unexpected error"
else:     # executed if no exceptions are raised
   print "File write completed successfully"
finally:  # clean-up actions, always executed
   if f:
      f.close()

9 같이 보기[ | ]

10 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}